FluorineFx.NET的認證(Authentication )與授權(quán)(Authorization)Flex與.NET互操作 九
比如說我們需要自定義一個認證與授權(quán)的方案,指定那些遠程服務(wù)上的那些方法將要被認證或授權(quán)以及授權(quán)用戶角色組等,我們就需要自定義一個 LoginCommand并實現(xiàn)ILoginCommand接口或者繼承于 FluorineFx.Security.GenericLoginCommand(此類實現(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()來實現(xiàn)驗證,具體的驗證規(guī)則我們可以自定義(重寫方法的實現(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 //用戶標識
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"兩個角色組之一,否則則不能通過驗證。要實現(xiàn)授權(quán)則是通過DoAuthorization()方法來實現(xiàn),我們同樣可以重寫實現(xiàn)以滿足自己的需求。
除此之外還需要service-config.xml,指定通過那一個LoginCommand來執(zhí)行認證與授權(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)用認證與授權(quán),同樣需要提供一個遠程服務(wù)接口,并為該接口添加RemotingServiceAttribute描述:
2 {
3 /// <summary>
4 /// 遠程服務(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來訪問遠程對象,F(xiàn)lex的訪問配置如下代碼塊:
<mx:method name="Login" result="onLoginResult(event)" fault="onLoginFault(event)"/>
</mx:RemoteObject>
通過配置RemoteObject指定訪問login這個配置的遠程服務(wù),服務(wù)里配置了一遠程方法Login,并分別定義了訪問成功和失敗的處理函數(shù)。上面的RemoteObject訪問的目的地為login配置的目的地,詳細配置在remoting-config.xml里,如下:
<destination id="login">
<properties>
<source>FlexDotNet.ServiceLibrary.Authentication.LoginService</source>
</properties>
</destination>
FlexDotNet.ServiceLibrary.Authentication.LoginService為自定義的一個遠程服務(wù)(標記為RemotingService)接口,通過配置訪問目的地,F(xiàn)lex遠程對象組件利用此目的地通過FluorineFx網(wǎng)關(guān)調(diào)用遠程服務(wù)接口方法。
布局Flex界面,模擬登錄驗證的調(diào)用,F(xiàn)lex通過setCredentials()方法請求,詳細如下代碼塊:
{
loginService.logout();
loginService.setCredentials(txtName.text,txtPassword.text);
loginService.Login();
}
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function Login():void
{
loginService.logout();
loginService.setCredentials(txtName.text,txtPassword.text);
loginService.Login();
}
private function Logout():void
{
loginService.logout();
}
private function onLoginResult(evt:ResultEvent):void
{
var result:Boolean = evt.result as Boolean;
if(result)
Alert.show("登錄驗證成功");
}
private function onLoginFault(evt:FaultEvent):void
{
Alert.show(ObjectUtil.toString(evt.fault),"登錄驗證失敗");
}
]]>
</mx:Script>
<mx:RemoteObject id="loginService" destination="login">
<mx:method name="Login" result="onLoginResult(event)" fault="onLoginFault(event)"/>
</mx:RemoteObject>
<mx:Panel x="124" y="102" width="250" height="200" layout="absolute" fontSize="12" title="用戶登錄">
<mx:Label x="19" y="28" text="用戶名:"/>
<mx:Label x="19" y="72" text="密 碼:"/>
<mx:TextInput x="75" y="26" width="131" id="txtName"/>
<mx:TextInput x="75" y="69" width="131" id="txtPassword" displayAsPassword="true"/>
<mx:HBox x="75" y="107" width="131" height="30">
<mx:Button label="登 錄" click="Login()"/>
<mx:Button label="清 空"/>
</mx:HBox>
</mx:Panel>
</mx:Application>相關(guān)文章
Flex與.NET互操作 使用HttpService、URLReqeust和URLLoader加載/傳輸數(shù)據(jù)
在前兩篇文章中分別介紹了Flex與.NET的WebService之間的數(shù)據(jù)交互通信知識,本文將介紹另外一種加載數(shù)據(jù)以及發(fā)起請求的方式。2009-06-06

