kbmMW User authentication
任何信息系统的一个非常重要的部分是能够对用户进行身份验证。 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的更多相关文章
- KbmMW 4.5 发布
We are happy to announce the release of kbmMW v. 4.50.00 Professional, Enterprise and CodeGear Editi ...
- KbmMW 4.40.00 正式版发布
经过快3个月的测试,kbmmw 4.40 正式版终于在圣诞节前发布了. We are happy to announce the availability of a new kbmMW release ...
- KbmMW 4.40.00 测试发布
经过漫长的等待,支持移动开发的kbmmw 4.40.00 终于发布了,这次不但支持各个平台的开发, 而且增加了认证管理器等很多新特性,非常值得升级.具体见下表. 4.40.00 BETA 1 Oct ...
- 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 ...
- Atitit HTTP 认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结
Atitit HTTP认证机制基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)attilax总结 1.1. 最广泛使用的是基本验证 ( ...
- [转]Web APi之认证(Authentication)及授权(Authorization)【一】(十二)
本文转自:http://www.cnblogs.com/CreateMyself/p/4856133.html 前言 无论是ASP.NET MVC还是Web API框架,在从请求到响应这一过程中对于请 ...
- smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')解决办法
raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'Error: authentica ...
- SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed
环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...
- 执行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,则先执行如下命令 ...
随机推荐
- 移动端自动化测试-Mac-IOS-Appium环境搭建
第一步 安装JDK,本机如果带有1.7及以上版本的,则可忽略此安装步骤. 百度下载JDK,并配置环境变量 vim ~/.bash_profile 检查是否安装成功 java -version 第二步 ...
- oracle查询视图归属于哪个用户
select OWNER from ALL_VIEWS where VIEW_NAME='视图名';
- 【Linux】bash shell学习
Bash Shell Linux系统的合法shell都写入/etc/shells这个文件,默认使用的shell版本称为“Bourne Again Shell(简称bash)” 用户登录时系统会分配一个 ...
- Jmeter的使用简介及实例
一.安装及配置环境1.安装 java环境 该软件需要java环境,安装jdk,在百度自行查找安装 环境变量配置:变量名JAVA_HOME 值:jdk的安装路径 ...
- Vue 项目骨架屏注入与实践
作为与用户联系最为密切的前端开发者,用户体验是最值得关注的问题.关于页面loading状态的展示,主流的主要有loading图和进度条两种.除此之外,越来越多的APP采用了“骨架屏”的方式去展示未加载 ...
- 前端基础之html常用标签
前言: 1.在B-S模式下,server服务端和客户端之间 使用http协议(规定 客户端应该怎么请求服务端,服务端应该怎么响应)通信: 2.传输过程 浏览器 向服务端发起 post/get请求 服务 ...
- ActiveMQ 处理不同类型的消息
ActiveMQ 中的消息都继承自 org.apache.activemq.command.BaseCommand 类. broker 处理消息的调用栈如下: TransportConnection ...
- 把旧系统迁移到.Net Core 2.0 日记(9) -- T4 Template
想着用T4 Template 自动生成代码,省了功夫. 发现T4 Template 挺笨的. 我开始这样写是会报错的 <# var modualName = "CRM" ...
- C# [IPA]IOS In App Purchase(内购)验证(asp.net 版本)
之前没有做过IOS 内购服务器验证这块,所以找了不少参考资料,网上大多php和java版本,然后自己搞了一个C#版本,希望能给大家一些参考,下面步入正题 在客户端向苹果购买成功之后,我们需要进行二次验 ...
- JSP页面间的传值方法总结
JSP 页面间传递参数是项目中经常需要的,这应该算是 web 基本功吧.试着将各种方式总结下来,需要时可以进行权衡利弊选择最合适的方式.下面来一起看看详细的介绍: 1. URL 链接后追加参数 ? 1 ...