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

简介:

FluorineFx.NET的认证(Authentication )与授权(Authorization)和ASP.NET中的大同小异,核实用户的身份既为认证,授权则是确定一个用户是否有某种执行权限,应用程序可根据用户信息授予和拒绝执行。FluorineFx.NET的认证和授权使用.Net Framework基于角色的安全性的支持。

      比如说我们需要自定义一个认证与授权的方案,指定那些远程服务上的那些方法将要被认证或授权以及授权用户角色组等,我们就需要自定义一个LoginCommand并实现ILoginCommand接口或者继承于FluorineFx.Security.GenericLoginCommand(此类实现了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  }
复制代码
复制代码

 

      网关通过调用该接口中的方法DoAuthentication()来实现验证,具体的验证规则我们可以自定义(重写方法的实现)。

复制代码
复制代码
 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               // 用户标识
12              GenericIdentity identity  =   new  GenericIdentity(username);
13               // 角色数组
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"两个角色组之一,否则则不能通过验证。要实现授权则是通过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  namespace  FlexDotNet.ServiceLibrary.Authentication
 2  {
 3       ///   <summary>
 4       ///  远程服务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来访问远程对象,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()方法请求,详细如下代码块:

复制代码
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("登录验证成功");
            }
            
            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
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2013/05/13/3076375.html ,如需转载请自行联系原作者
相关文章
|
缓存 JSON 算法
(三).NET Core WebAPI集成JWT,实现身份验证
前两篇文章给大家介绍了在.NET Core中如何使用Swagger的文章,那今天给大家分享一下JWT 在做接口开发的同学可能都有感受,我的接口如何保护的问题,如果没有身份验证,那不是接口完全暴露在外面,任意使人调用,这显然不是我们想要的一种结果。当然做身份验证的方式有多种,今天给大家讲一种比较流行了,标准的身份验证JWT 什么是JWT?
|
8月前
|
开发框架 中间件 .NET
Asp.Net Core认证-Jwt-基础篇
Asp.Net Core认证-Jwt-基础篇
123 0
|
开发框架 算法 安全
在 Asp.Net Core 中什么是认证和授权
认证(Authentication) 和 授权(Authorization)在 Asp.Net core 充当了两个不同的职责。有的老伙计在理解的时候还存在误解。本文我们将会通过一些简单的例子来说明这两个概念。
123 0
在 Asp.Net Core 中什么是认证和授权
|
存储 开发框架 .NET
ASP.NET Core 中jwt授权认证的流程原理
ASP.NET Core 中jwt授权认证的流程原理
227 0
ASP.NET Core 中jwt授权认证的流程原理
|
存储 开发框架 安全
ASP.NET Core 使用JWT 自定义角色/策略授权需要实现的接口
ASP.NET Core 使用JWT 自定义角色/策略授权需要实现的接口
358 0
|
存储 前端开发 JavaScript
.net core实践系列之SSO-跨域实现
.net core实践系列之SSO-跨域实现
196 0
.net core实践系列之SSO-跨域实现
|
Web App开发 .NET 数据安全/隐私保护
一起谈.NET技术,ASP.NET身份验证机制membership入门——项目
  前面说了很多关于membership的内容,感觉内容有点凌乱,内容都是一个个知识点,下面我们通过一个小的项目,来把所有的相关内容串一下。   首先描述一下需求:   我们要做一个最简单的网站。有三类用户:匿名用户,员工,管理员,网站结构如下:        admin目录下的页面只允许admin角色的用户访问,employee目录下的页面只允许emp角色的用户访问。
1084 0
Asp.Net Core基于JWT认证的数据接口网关Demo
Asp.Net Core基于JWT认证的数据接口网关Demo 近日,应一位朋友的邀请写了个Asp.Net Core基于JWT认证的数据接口网关Demo。朋友自己开了个公司,接到的一个升级项目,客户要求用Aps.Net Core做数据网关服务且基于JWT认证实现对前后端分离的数据服务支持,于是想到我一直做.Net开发,问我是否对.Net Core有所了解?能不能做个简单Demo出来看看?我说,分道扬镳之后我不是调用别人的接口就是提供接口给别人调用,于是便有了以下示例代码。
4710 0
|
Web App开发 .NET API
5.1基于JWT的认证和授权「深入浅出ASP.NET Core系列」
原文:5.1基于JWT的认证和授权「深入浅出ASP.NET Core系列」 希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,码字辛苦,如果你吃了蛋觉得味道不错,希望点个赞,谢谢关注。
1914 0