FluorineFx.NET的認(rèn)證(Authentication )與授權(quán)(Authorization)Flex與.NET互操作 九
比如說我們需要自定義一個(gè)認(rèn)證與授權(quán)的方案,指定那些遠(yuǎn)程服務(wù)上的那些方法將要被認(rèn)證或授權(quán)以及授權(quán)用戶角色組等,我們就需要自定義一個(gè) LoginCommand并實(shí)現(xiàn)ILoginCommand接口或者繼承于 FluorineFx.Security.GenericLoginCommand(此類實(shí)現(xiàn)了ILoginCommand接口)基類。接口定義如下:
2 {
3 public interface ILoginCommand
4 {
5 IPrincipal DoAuthentication(string username, Hashtable credentials);
6 bool DoAuthorization(IPrincipal principal, IList roles);
7 bool Logout(IPrincipal principal);
8 void Start();
9 void Stop();
10 }
11 }
網(wǎng)關(guān)通過調(diào)用該接口中的方法DoAuthentication()來實(shí)現(xiàn)驗(yàn)證,具體的驗(yàn)證規(guī)則我們可以自定義(重寫方法的實(shí)現(xiàn))。
2 /// 自定義 LoginCommand
3 /// </summary>
4 public class LoginCommand : GenericLoginCommand
5 {
6 public override IPrincipal DoAuthentication(string username, Hashtable credentials)
7 {
8 string password = credentials["password"] as string;
9 if (username == "admin" && password == "123456")
10 {
11 //用戶標(biāo)識
12 GenericIdentity identity = new GenericIdentity(username);
13 //角色數(shù)組
14 GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
15 return principal;
16 }
17 else
18 {
19 return null;
20 }
21 }
22 }
如上面代碼塊,檢測用戶是不是屬于"admin"和"privilegeduser"兩個(gè)角色組之一,否則則不能通過驗(yàn)證。要實(shí)現(xiàn)授權(quán)則是通過DoAuthorization()方法來實(shí)現(xiàn),我們同樣可以重寫實(shí)現(xiàn)以滿足自己的需求。
除此之外還需要service-config.xml,指定通過那一個(gè)LoginCommand來執(zhí)行認(rèn)證與授權(quán),以及要被授權(quán)的方法和角色組,login-command的class指向自定義的LoginCommand.
<security-constraint id="privileged-users">
<auth-method>Login</auth-method>
<roles>
<role>admin</role>
<role>privilegeduser</role>
</roles>
</security-constraint>
<login-command class="FlexDotNet.ServiceLibrary.Authentication.LoginCommand" server="asp.net"/>
</security>
要使Flex能夠調(diào)用認(rèn)證與授權(quán),同樣需要提供一個(gè)遠(yuǎn)程服務(wù)接口,并為該接口添加RemotingServiceAttribute描述:
2 {
3 /// <summary>
4 /// 遠(yuǎn)程服務(wù)LoginService
5 /// </summary>
6 [RemotingService]
7 public class LoginService
8 {
9 public LoginService()
10 { }
11
12 /// <summary>
13 /// 登錄
14 /// </summary>
15 /// <returns></returns>
16 public bool Login(string userName,string password)
17 {
18 if (userName == "admin" && password == "123456")
19 {
20 //do other
21 return true;
22 }
23 else
24 {
25 //do other
26 return false;
27 }
28 }
29
30 /// <summary>
31 /// 注銷
32 /// </summary>
33 /// <param name="userName">用戶名</param>
34 /// <returns></returns>
35 public bool Logout(string userName)
36 {
37 GenericIdentity identity = new GenericIdentity(userName);
38 GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
39
40 if (new LoginCommand().Logout(principal))
41 return true;
42 return false;
43 }
44 }
45 }
在Flex或Flash端就可以通過RemoteObject來訪問遠(yuǎn)程對象,F(xiàn)lex的訪問配置如下代碼塊:
<mx:method name="Login" result="onLoginResult(event)" fault="onLoginFault(event)"/>
</mx:RemoteObject>
通過配置RemoteObject指定訪問login這個(gè)配置的遠(yuǎn)程服務(wù),服務(wù)里配置了一遠(yuǎn)程方法Login,并分別定義了訪問成功和失敗的處理函數(shù)。上面的RemoteObject訪問的目的地為login配置的目的地,詳細(xì)配置在remoting-config.xml里,如下:
<destination id="login">
<properties>
<source>FlexDotNet.ServiceLibrary.Authentication.LoginService</source>
</properties>
</destination>
FlexDotNet.ServiceLibrary.Authentication.LoginService為自定義的一個(gè)遠(yuǎn)程服務(wù)(標(biāo)記為RemotingService)接口,通過配置訪問目的地,F(xiàn)lex遠(yuǎn)程對象組件利用此目的地通過FluorineFx網(wǎng)關(guān)調(diào)用遠(yuǎn)程服務(wù)接口方法。
布局Flex界面,模擬登錄驗(yàn)證的調(diào)用,F(xiàn)lex通過setCredentials()方法請求,詳細(xì)如下代碼塊:
{
loginService.logout();
loginService.setCredentials(txtName.text,txtPassword.text);
loginService.Login();
}















































相關(guān)文章
Flex 編程注意之Flex Complier參數(shù)
由于上一篇文章《Flex編程注意之Namespace的用法》引出了Flex編譯時(shí)自帶的兩個(gè)參數(shù):-namespace -include-namespace,正好想到可以寫一篇關(guān)于Flex Complier時(shí)的一些自定義參數(shù)。2009-07-07Flex與.NET互操作 使用FileReference+HttpHandler實(shí)現(xiàn)文件上傳/下載
Flex與.NET互操作 使用FileReference+HttpHandler實(shí)現(xiàn)文件上傳/下載2009-06-06Flex 編程注意之性能優(yōu)化、垃圾回收的一些總結(jié)
自從開始做Flex、ActionScript 3.0的項(xiàng)目,我就一直與垃圾回收、性能優(yōu)化這些問題打交道,因此也總結(jié)了一些優(yōu)化的方案,同時(shí)在一些QQ群中也得到了一些“高人”的指點(diǎn),因此將此內(nèi)容記錄一下。2009-07-07XML TO ArrayCollection 兩種實(shí)現(xiàn)方式
最近研究FLEX,在網(wǎng)上看過100遍同一篇文章,結(jié)果測試不通,不知道到底能不能跑通,最后翻資料自己試驗(yàn)處理。2009-06-06Flex與.NET互操作 了解FluorineFx的環(huán)境配置(遠(yuǎn)程對象、網(wǎng)關(guān)、通道、目的地)
Flex中的遠(yuǎn)程對象訪問,也就是服務(wù)端提供一個(gè)遠(yuǎn)程服務(wù)對象(RemotingService Object),在Flex客戶端通過相應(yīng)的訪問技術(shù)去調(diào)用遠(yuǎn)程對象的過程。2009-06-06Flex與.NET互操作(十一):FluorineFx.Net的及時(shí)通信應(yīng)用(Remote Procedure Call
FluorineFx.NET提供了完善的RPC(Remote Procedure Call)功能,無論是通過Flash還是Flex開發(fā)的客戶端應(yīng)用(.swf)都可以非常簡單方便的采用RPC的方式調(diào)用.NET的服務(wù)器端方法2009-06-06