日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Flex与.NET互操作(九):FluorineFx.NET的认证(Authentication )与授权(Authorization)

發布時間:2024/9/20 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flex与.NET互操作(九):FluorineFx.NET的认证(Authentication )与授权(Authorization) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?FluorineFx.NET的認證(Authentication )與授權(Authorization)和ASP.NET中的大同小異,核實用戶的身份既為認證,授權則是確定一個用戶是否有某種執行權限,應用程序可根據用戶信息授予和拒絕執行。FluorineFx.NET的認證和授權使用.Net Framework基于角色的安全性的支持。

??????比如說我們需要自定義一個認證與授權的方案,指定那些遠程服務上的那些方法將要被認證或授權以及授權用戶角色組等,我們就需要自定義一個LoginCommand并實現ILoginCommand接口或者繼承于FluorineFx.Security.GenericLoginCommand(此類實現了ILoginCommand接口)基類。接口定義如下:

1?namespaceFluorineFx.Security2?{3?publicinterfaceILoginCommand4?{5?IPrincipal?DoAuthentication(stringusername,?Hashtable?credentials);6?boolDoAuthorization(IPrincipal?principal,?IList?roles);7?boolLogout(IPrincipal?principal);8?voidStart();9?voidStop();10?}11?}

??????網關通過調用該接口中的方法DoAuthentication()來實現驗證,具體的驗證規則我們可以自定義(重寫方法的實現)。

1?///<summary>2?///自定義?LoginCommand3?///</summary>4?publicclassLoginCommand?:?GenericLoginCommand5?{6?publicoverrideIPrincipal?DoAuthentication(stringusername,?Hashtable?credentials)7?{8?stringpassword?=credentials["password"]?asstring;9?if(username?=="admin"&&password?=="123456")10?{11?//用戶標識12?GenericIdentity?identity?=newGenericIdentity(username);13?//角色數組14?GenericPrincipal?principal?=newGenericPrincipal(identity,?newstring[]?{?"admin",?"privilegeduser"});15?returnprincipal;16?}17?else18?{19?returnnull;20?}21?}22?}

??????如上面代碼塊,檢測用戶是不是屬于"admin"和"privilegeduser"兩個角色組之一,否則則不能通過驗證。要實現授權則是通過DoAuthorization()方法來實現,我們同樣可以重寫實現以滿足自己的需求。

??????除此之外還需要service-config.xml,指定通過那一個LoginCommand來執行認證與授權,以及要被授權的方法和角色組,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能夠調用認證與授權,同樣需要提供一個遠程服務接口,并為該接口添加RemotingServiceAttribute描述:

1?namespaceFlexDotNet.ServiceLibrary.Authentication2?{3?///<summary>4?///遠程服務LoginService5?///</summary>6?[RemotingService]7?publicclassLoginService8?{9?publicLoginService()10?{?}11?12?///<summary>13?///登錄14?///</summary>15?///<returns></returns>16?publicboolLogin(stringuserName,stringpassword)17?{18?if(userName?=="admin"&&password?=="123456")19?{20?//do?other21?returntrue;22?}23?else24?{25?//do?other26?returnfalse;27?}28?}29?30?///<summary>31?///注銷32?///</summary>33?///<param?name="userName">用戶名</param>34?///<returns></returns>35?publicboolLogout(stringuserName)36?{37?GenericIdentity?identity?=newGenericIdentity(userName);38?GenericPrincipal?principal?=newGenericPrincipal(identity,?newstring[]?{?"admin",?"privilegeduser"});39?40?if(newLoginCommand().Logout(principal))41?returntrue;42?returnfalse;43?}44?}45?}

??????在Flex或Flash端就可以通過RemoteObject來訪問遠程對象,Flex的訪問配置如下代碼塊:

<mx:RemoteObject?id="loginService"destination="login"><mx:method?name="Login"result="onLoginResult(event)"fault="onLoginFault(event)"/></mx:RemoteObject>

??????通過配置RemoteObject指定訪問login這個配置的遠程服務,服務里配置了一遠程方法Login,并分別定義了訪問成功和失敗的處理函數。上面的RemoteObject訪問的目的地為login配置的目的地,詳細配置在remoting-config.xml里,如下:

<destination?id="login"><properties>?

????????????<source>FlexDotNet.ServiceLibrary.Authentication.LoginService</source></properties></destination>

??????FlexDotNet.ServiceLibrary.Authentication.LoginService為自定義的一個遠程服務(標記為RemotingService)接口,通過配置訪問目的地,Flex遠程對象組件利用此目的地通過FluorineFx網關調用遠程服務接口方法。

??????布局Flex界面,模擬登錄驗證的調用,Flex通過setCredentials()方法請求,詳細如下代碼塊:

privatefunction?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("登錄驗證成功");????????????}????????????????????????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> services-config.xml<?xml version="1.0" encoding="utf-8" ?>?<services-config><services><service-include file-path="remoting-config.xml" /></services><!-- Custom authentication --><security><security-constraint id="privileged-users"><auth-method>Custom</auth-method><roles><role>admin</role><role>privilegeduser</role></roles></security-constraint>?<login-command class="FlexDotNet.ServiceLibrary.Authentication.LoginCommand" server="asp.net"/></security><channels><channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel"><endpoint uri="rtmp://localhost:2086/Web/Gateway.aspx" class="flex.messaging.endpoints.AMFEndpoint"/><properties><!-- <legacy-collection>true</legacy-collection> --></properties></channel-definition><channel-definition id="my-rtmp" class="mx.messaging.channels.RTMPChannel"><endpoint uri="rtmp://localhost:2086/Web/Gateway.aspx" class="flex.messaging.endpoints.RTMPEndpoint"/><properties><idle-timeout-minutes>20</idle-timeout-minutes></properties></channel-definition></channels></services-config>

總結

以上是生活随笔為你收集整理的Flex与.NET互操作(九):FluorineFx.NET的认证(Authentication )与授权(Authorization)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。