欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

FluorineFx.NET的認(rèn)證(Authentication )與授權(quán)(Authorization)Flex與.NET互操作 九

 更新時(shí)間:2009年06月15日 20:15:04   作者:  
FluorineFx.NET的認(rèn)證(Authentication )與授權(quán)(Authorization)和ASP.NET中的大同小異,核實(shí)用戶的身份既為認(rèn)證,授權(quán)則是確定一個(gè)用戶是否有某種執(zhí)行權(quán)限
應(yīng)用程序可根據(jù)用戶信息授予和拒絕執(zhí)行。FluorineFx.NET的認(rèn)證和授權(quán)使用.Net Framework基于角色的安全性的支持。

比如說我們需要自定義一個(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接口)基類。接口定義如下:

 1 namespace FluorineFx.Security
 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))。

 1 /// <summary>
 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>
      
<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描述:

 1 namespace FlexDotNet.ServiceLibrary.Authentication
 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:RemoteObject id="loginService" destination="login">
    
<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ì)如下代碼塊:

private function Login():void
{
    loginService.logout();
    loginService.setCredentials(txtName.text,txtPassword.text);
    loginService.Login();
}
<?xml version="1.0" encoding="utf-8"?>
<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("登錄驗(yàn)證成功");
            }
            
            private function onLoginFault(evt:FaultEvent):void
            {
                Alert.show(ObjectUtil.toString(evt.fault),"登錄驗(yàn)證失敗");
            }
        ]]
>
    
</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)文章

最新評論