Delphi TDSAuthenticationManager的用法


本文整理自网络,侵删。

 
TDSAuthenticationManager的用法 
xe开始有了TDSAuthenticationManager,这个主要用来做用户认证,用法也很简单

服务器端
1.TDSAuthenticationManager有两个主要的事件

在这个事件里面,看看检测连上来的用户名,密码是否合法,valid如果置为false,这就为非法连接了,DSServer会立刻抛出异常后close连接。
另外,UserRoles的设计,我觉得比RO高明。

procedure TServerContainer1.DSAuthenticationManager1UserAuthenticate(   Sender: TObject; const Protocol, Context, User, Password: string;   
var

valid: Boolean;

UserRoles: TStrings; 
begin  
valid := User = 'zyz';     
if User = 'admin' then    
UserRoles.Add('admins'); 
end;

在这个事件里面,判断已经连接上来的用户,对ServerMethod的调用是否合法,注视里也写了,默认是如何检测是否合法的。
procedure TServerContainer1.DSAuthenticationManager1UserAuthorize(   Sender: TObject; EventObject: TDSAuthorizeEventObject;   var valid: Boolean); 
begin  
{ TODO : Authorize a user to execute a method.     Use values from EventObject such as UserName, UserRoles, AuthorizedRoles and 
DeniedRoles.     Use DSAuthenticationManager1.Roles to define Authorized and Denied roles     for particular server methods. }  
//valid := True; 
end;

上面我说UserRoles的设计比较高明,主要还是因为这个UserRole的设计用到了java的那种注释类的技术,比如服务器上这么定义一个方法

?12 [TRoleAuth('admins')] function EchoString(Value: string): string;

这样定义后,就算不写DSAuthenticationManager1UserAuthorize,TDSAuthenticationManager也会自动帮你检查该角色是否有权利调用该ServerMethod。RTTI估计是学Java的Annotation才增加了TCustomAttribute。


2.客户端
客户端很简单了,设置SQLConnection的DSAuthUser和DSAuthPassword就行了。

相关阅读 >>

Delphi从全路径中分离路径,有'\'

Delphi 获取分解时间日期的年月日

Delphi 学习 sql 语句 - select(8): 分组条件

Delphi 虚拟桌面服务程序

Delphi 设置webbrowser 代理服务器 与 useragent

Delphi 延时的4种方法

Delphi7 写过卡巴主动防御服务端

Delphi响应wmi事件(响应网线断开)

Delphi webbrowser1遍历网页中的图片

Delphi 安卓动态申请权限清单类

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...