任何信息系统的一个非常重要的部分是能够对用户进行身份验证。 kbmMW在这里提供了非常强大的机制。 TkbmMWSimpleClient提供简单的用户身份验证机制,您可以在连接到应用程序服务器时传递UserName和Password。 但是,要创建最灵活和最强大的身份验证机制,有必要编写一些代码......这里的标准方法是使用令牌(tokens)来验证客户端请求,这样用户名和密码就不会在所有客户端请求上传递,这种方式是更安全的。

服务器生成令牌而不是客户端。 客户只是接收回传的内容。 您可以使用clientident.customdata来存储用户的id,但是您也可以使用它来返回在“perm”中定义的内容。 我认为这种方法更好,所以看下面的代码:

unit Unit11;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB,
kbmMWServer, kbmMemTable, kbmMWSecurity, kbmMWExceptions,kbmMWuniDAC,
DBAccess, Uni, kbmMWCustomConnectionPool; type
TForm11 = class(TForm)
mwServer: TkbmMWServer;
Memo1: TMemo;
tbmusers: TkbmMemTable;
mwcpool: TkbmMWUNIDACConnectionPool;
UniConnection1: TUniConnection;
procedure mwServerAuthenticate(Sender: TObject;
ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
var AMessage: string);
private
function IsUserLegit(UserName,password:string;var customdata,token:string):Boolean;
function IsTokenLegit(token:string):Boolean;
public
{ Public declarations }
end; var
Form11: TForm11; implementation {$R *.dfm} function TForm11.IsTokenLegit(token: string): Boolean;
begin
tbmusers.Lock;
try
if tbmusers.Locate('token',token,[]) then
begin
Result:=true;
tbmusers.Edit;
tbmusers.FieldByName('exppiry').AsDateTime:=Now+(/(*));//当前时间+15分钟
tbmusers.Post;
end
else
Result:=false;
finally
tbmusers.Unlock;
end;
end; function TForm11.IsUserLegit(UserName, password: string; var customdata,
token: string): Boolean;
var
conn:TkbmMWUNIDACConnection;
qry:TUniQuery;
begin
Result := False;
qry := TUniQuery.Create(nil);
conn := TkbmMWUNIDACConnection(mwcpool.GetBestConnection(true, -, nil, ));
try
qry.Connection := conn.Database;
qry.SQL.Add('select id from employees where name=:username and password=:password');
qry.ParamByName('username').AsString := UserName;
qry.ParamByName('password').AsString := password;
qry.Open;
if qry.Eof then
begin
Result := False;//用户名或密码错误
end
else
begin
//用户与密码正确,建立token并返回customdata.
customdata := qry.Fields[].AsString;
//建立简单的token
token := customdata + IntToStr(GetTickCount);
Result := True;
end;
finally
FreeAndNil(qry);
FreeAndNil(conn);
end;
end; procedure TForm11.mwServerAuthenticate(Sender: TObject;
ClientIdent: TkbmMWClientIdentity; var Perm: TkbmMWAccessPermissions;
var AMessage: string);
var
userdata,
token: string;
begin // no access by default
perm:=[];
//check to see if user already logged in
if clientident.token='' then
begin
memo1.lines.add(clientident.password);
if IsUserLegit(clientident.username,clientident.password,userdata,token) then
begin
clientident.Data:=userdata;
clientident.Token:=token;
perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute];
try
tbmusers.Lock;
tbmusers.Insert;
tbmusers.FieldByName('id').AsString:=userdata;
tbmusers.FieldByName('token').AsString:=token;
tbmusers.FieldByName('expiry').AsDateTime:=now+(/(*));
tbmusers.Post;
finally
tbmusers.Unlock;
end;
end
else
raise EkbmMWAuthException.Create(,'Username or Password Invalid');
end
else
begin
if IsTokenLegit(clientident.token) then
perm:=[mwapRead, mwapWrite, mwapDelete, mwapExecute]
else
raise EkbmMWAuthException.Create(,'Timeout');
end;
clientident.Username:='';
clientident.Password:='';
end; end.

kbmMW User authentication的更多相关文章

  1. KbmMW 4.5 发布

    We are happy to announce the release of kbmMW v. 4.50.00 Professional, Enterprise and CodeGear Editi ...

  2. KbmMW 4.40.00 正式版发布

    经过快3个月的测试,kbmmw 4.40 正式版终于在圣诞节前发布了. We are happy to announce the availability of a new kbmMW release ...

  3. KbmMW 4.40.00 测试发布

    经过漫长的等待,支持移动开发的kbmmw 4.40.00 终于发布了,这次不但支持各个平台的开发, 而且增加了认证管理器等很多新特性,非常值得升级.具体见下表. 4.40.00 BETA 1 Oct ...

  4. WCF : 修复 Security settings for this service require Windows Authentication but it is not enabled for the IIS application that hosts this service 问题

    摘要 : 最近遇到了一个奇怪的 WCF 安全配置问题, WCF Service 上面配置了Windows Authentication. IIS上也启用了 Windows Authentication ...

  5. Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结

    Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...

  6. [转]Web APi之认证(Authentication)及授权(Authorization)【一】(十二)

    本文转自:http://www.cnblogs.com/CreateMyself/p/4856133.html 前言 无论是ASP.NET MVC还是Web API框架,在从请求到响应这一过程中对于请 ...

  7. smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')解决办法

    raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'Error: authentica ...

  8. SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed

    环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...

  9. 执行ssh-add时出现Could not open a connection to your authentication agent

    若执行ssh-add /path/to/xxx.pem是出现这个错误:Could not open a connection to your authentication agent,则先执行如下命令 ...

随机推荐

  1. Fragment的onCreateView和onActivityCreate之间的区别(转)

    看了有关这个问题的几篇博文,几乎都是引用了stackoverflow上的一个回答: 问题: I know that a fragment’s view hierarchy has to be infl ...

  2. Activiti搭建

    Activiti搭建 前期准备: JDK+Eclipse+Tomcat+Maven的安装与配置 参考:http://blog.csdn.net/zhshulin/article/details/307 ...

  3. git:not a git repository (or any of the parent directories)

    我用git add file添加文件时出现了这样错误: fatal: Not a git repository (or any of the parent directories): .git 提示说 ...

  4. win10下使用python访问vmbox中的redis

    了解到redis没有windows的官方支持,所以在vmbox中的ubuntu装了redis#在ubuntu中 #搜索redis相关软件信息 apt-cache search ^redis #不清楚为 ...

  5. Git的各种状态

    考:http://blog.csdn.net/wirelessqa/article/details/19548057 按照文件的存放位置分: 在你自建的Git本地仓库中,有三个区域:本地目录.暂存区. ...

  6. python中RabbitMQ的使用(路由键)

    1.简介 当我们希望每个接收端接收各自希望的消息时,我们可以使用路由键,此时交换机的类型为direct. 2.工作原理 每个接收端的消息队列在绑定交换机的时候,可以设定相应的路由键. 发送端通过交换机 ...

  7. git status 查看当前修改文件

    可以查看当前已经修改的文件.

  8. MapReduce(三)

    MapReduce(三) MapReduce(三): 1.关于倒叙排序前10名 1)TreeMap根据key排序 2)TreeSet排序,传入一个对象,排序按照类中的compareTo方法排序 2.写 ...

  9. 2.Liunx 系统设置

    1.基本命令:clear 2.环境变量: alias .export 大纲 系统管理命令 1.用户管理:adduser .passwd. userdel 2.用户组管理:groupadd.groupd ...

  10. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...