专著于富媒体技术~
本站某些作品来源于互联网,如果侵犯了您的利益,请留言说明!
本站某些作品来源于互联网,如果侵犯了您的利益,请留言说明!
比如说我们需要自定义一个认证与授权的方案,指定那些远程服务上的那些方法将要被认证或授权以及授权用户角色组等,我们就需要自定义一个LoginCommand并实现ILoginCommand接口或者继承于FluorineFx.Security.GenericLoginCommand(此类实现了ILoginCommand接口)基类。接口定义如下:
namespace FluorineFx.Security
{
public interface ILoginCommand
{
IPrincipal DoAuthentication(string username, Hashtable credentials);
bool DoAuthorization(IPrincipal principal, IList roles);
bool Logout(IPrincipal principal);
void Start();
void Stop();
}
}
{
public interface ILoginCommand
{
IPrincipal DoAuthentication(string username, Hashtable credentials);
bool DoAuthorization(IPrincipal principal, IList roles);
bool Logout(IPrincipal principal);
void Start();
void Stop();
}
}
网关通过调用该接口中的方法DoAuthentication()来实现验证,具体的验证规则我们可以自定义(重写方法的实现)。
/// <summary>
/// 自定义 LoginCommand
/// </summary>
public class LoginCommand : GenericLoginCommand
{
public override IPrincipal DoAuthentication(string username, Hashtable credentials)
{
string password = credentials["password"] as string;
if (username == "admin" && password == "123456")
{
//用户标识
GenericIdentity identity = new GenericIdentity(username);
//角色数组
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
return principal;
}
else
{
return null;
}
}
}
/// 自定义 LoginCommand
/// </summary>
public class LoginCommand : GenericLoginCommand
{
public override IPrincipal DoAuthentication(string username, Hashtable credentials)
{
string password = credentials["password"] as string;
if (username == "admin" && password == "123456")
{
//用户标识
GenericIdentity identity = new GenericIdentity(username);
//角色数组
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
return principal;
}
else
{
return null;
}
}
}
如上面代码块,检测用户是不是属于"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>
<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描述:
namespace FlexDotNet.ServiceLibrary.Authentication
{
/// <summary>
/// 远程服务LoginService
/// </summary>
[RemotingService]
public class LoginService
{
public LoginService()
{ }
/// <summary>
/// 登录
/// </summary>
/// <returns></returns>
public bool Login(string userName,string password)
{
if (userName == "admin" && password == "123456")
{
//do other
return true;
}
else
{
//do other
return false;
}
}
/// <summary>
/// 注销
/// </summary>
/// <param name="userName">用户名</param>
/// <returns></returns>
public bool Logout(string userName)
{
GenericIdentity identity = new GenericIdentity(userName);
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
if (new LoginCommand().Logout(principal))
return true;
return false;
}
}
}
{
/// <summary>
/// 远程服务LoginService
/// </summary>
[RemotingService]
public class LoginService
{
public LoginService()
{ }
/// <summary>
/// 登录
/// </summary>
/// <returns></returns>
public bool Login(string userName,string password)
{
if (userName == "admin" && password == "123456")
{
//do other
return true;
}
else
{
//do other
return false;
}
}
/// <summary>
/// 注销
/// </summary>
/// <param name="userName">用户名</param>
/// <returns></returns>
public bool Logout(string userName)
{
GenericIdentity identity = new GenericIdentity(userName);
GenericPrincipal principal = new GenericPrincipal(identity, new string[] { "admin", "privilegeduser" });
if (new LoginCommand().Logout(principal))
return true;
return false;
}
}
}
在Flex或Flash端就可以通过RemoteObject来访问远程对象,Flex的访问配置如下代码块:
<mx:RemoteObject id="loginService" destination="login">
<mx:method name="Login" result="onLoginResult(event)" fault="onLoginFault(event)"/>
</mx:RemoteObject>
<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>
<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();
}
{
loginService.logout();
loginService.setCredentials(txtName.text,txtPassword.text);
loginService.Login();
}
完整的Flex端代码
<?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>
<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>
出处:Bēniaǒ成长笔记
转载时必须以链接形式注明出处及本声明!
牛C网推荐您再看看以下日志:
利用数据库模版创建方便部署的.Net项目调试环境
给初学者(web开发)的建议
Lucene.net 实现全文搜索
C#技巧-网页表单自动填写技术(gmail为例)
《Effective C#》之用委托实现回调
解决 ASP.NET 中目录访问权限的问题
技巧-ASP.NET技术获取IP与MAC地址的方法
分享C#动态生成文字图片解决方案
自定义分页控件用于DataGrid(使用SQL储存过程)
Visual Basic .NET处理Excle表格全接触
利用数据库模版创建方便部署的.Net项目调试环境
给初学者(web开发)的建议
Lucene.net 实现全文搜索
C#技巧-网页表单自动填写技术(gmail为例)
《Effective C#》之用委托实现回调
解决 ASP.NET 中目录访问权限的问题
技巧-ASP.NET技术获取IP与MAC地址的方法
分享C#动态生成文字图片解决方案
自定义分页控件用于DataGrid(使用SQL储存过程)
Visual Basic .NET处理Excle表格全接触
上一篇



