使用 IntraWeb (29) - 基本控件之 TIWAutherList、TIWAutherINI、TIWAutherEvent    


TIWAutherList  //通过一组户名与密码验证登陆TIWAutherINI   //通过记录户名与密码信息的 #Auth.ini 文件验证登陆TIWAutherEvent //通过其 OnCheck 事件验证登陆{作为站点级的验证, 验证控件应该是放在 ServerController 的窗体上, 并与其 Auther 属性关联.}


TIWAutherList 所在单元及继承链:
IWAutherList.TIWAutherList < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:


property List: TStrings//户名与密码表; 每行按 User=Pass 的格式输入property AutherPolicy: TAutherPolicy //该属性有两个选项 apRestrictAll(默认)、apRestrictNone(选这个表示不执行验证)property OnAuthenticate: TOnAuthenticate //验证成功后执行的事件


测试 TIWAutherList:


{在 ServerController 的窗体上放置 IWAutherList1, 然后双击该窗体(激活其 OnCreate 事件)}procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);begin  Self.Auther := IWAutherList1;  IWAutherList1.List.Add('aaa=111');  IWAutherList1.List.Add('bbb=222');  IWAutherList1.List.Values['ccc'] := '333';end;{这就好了, 如果在设计时完成上面工作会更方便}





TIWAutherINI 所在单元及继承链:
IWAutherINI.TIWAutherINI < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:


property AutherPolicy: TAutherPolicy //property OnAuthenticate: TOnAuthenticate //{它需要的 ini 文件须命名为 #Auth.ini(它会保证不被用户读取, 应该使用 UTF8 格式保存), 并且和程序文件放在同一目录(而非 wwwroot 下)}{其格式规范:--------------------[户名1]Password=密码1[户名2]Password=密码2...------------------------------}{建好文件, 放对地方, 再关联上 Auther 属性就可以了}



TIWAutherEvent 所在单元及继承链:
IWAutherEvent.TIWAutherEvent < TIWAutherBase < TComponent < TPersistent < TObject

主要成员:


property AutherPolicy: TAutherPolicy //property OnCheck: TOnCheck //就是在该事件中验证; 假如要从数据库验证就应该用这种方法property OnAuthenticate: TOnAuthenticate //{更多时候可能需要把验证函数写在 UserSessionUnit 单元(譬如通过数据库验证时), 这时应该保证 IWServerController.AuthBeforeNewSession = False(这也是默认值)}


测试 TIWAutherEvent:


{IWAutherEvent1 的 OnCheck 事件}function TIWServerController.IWAutherEvent1Check(const aUser, aPass: string): Boolean;begin  Result := aPass = aUser + '123'; //假如密码是: 用户名+123end;{需要保证关联到 Auther 属性}procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);begin  Auther := IWAutherEvent1;end;


还是上面的例子, 现在改成通过 TIWUserSession 的一个方法来验证:


{UserSessionUnit.pas}unit UserSessionUnit;interfaceuses  IWUserSessionBase, SysUtils, Classes;type  TIWUserSession = class(TIWUserSessionBase)  private  public    function MyCheck(const AUser, APass: string): Boolean;  end;implementation{$R *.dfm}{ TIWUserSession }function TIWUserSession.MyCheck(const AUser, APass: string): Boolean;begin  Result := APass.ToLower = AUser.ToLower + '123';end;end.{-------------------------------------------------}{ServerController.pas, 有注释的是自己添加的代码}unit ServerController;interfaceuses  SysUtils, Classes, IWServerControllerBase, IWBaseForm, HTTPApp,  UserSessionUnit, IWApplication, IWAppForm, IW.Browser.Browser, IWAutherEvent, IWAutherINI, IWAutherBase, IWAutherList;type  TIWServerController = class(TIWServerControllerBase)    IWAutherEvent1: TIWAutherEvent;    procedure IWServerControllerBaseNewSession(ASession: TIWApplication);    procedure IWServerControllerBaseCreate(Sender: TObject);    function IWAutherEvent1Check(const aUser, aPass: string): Boolean;  private  public  end;function UserSession: TIWUserSession;function IWServerController: TIWServerController;implementation{$R *.dfm}uses  IWInit, IWGlobal;function IWServerController: TIWServerController;begin  Result := TIWServerController(GServerController);end;function UserSession: TIWUserSession;begin  Result := TIWUserSession(WebApplication.Data);end;{IWAutherEvent1 的 OnCheck 事件, 调用 UserSessionUnit.TIWUserSession 的验证函数}function TIWServerController.IWAutherEvent1Check(const aUser, aPass: string): Boolean;begin  Result := UserSession.MyCheck(aUser, aPass);end;{OnCreate 事件, 这个关联可以在设计时做}procedure TIWServerController.IWServerControllerBaseCreate(Sender: TObject);begin  Auther := IWAutherEvent1;end;procedure TIWServerController.IWServerControllerBaseNewSession(ASession: TIWApplication);begin  ASession.Data := TIWUserSession.Create(nil, ASession);end;initializationTIWServerController.SetServerControllerClass;end.



©著作权归作者所有:来自51CTO博客作者JLee79的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 听付彪说视觉验证
  2. JavaScript:数学对象,随机生成数字验证码并且随机添加字体颜色
  3. jQuery常用操作; jQuery中的$.ajax方法; Vue基本术语与插值语法---
  4. 短信验证码服务平台哪个好?
  5. 身份验证和权限管理---Openshift3.9学习系列第三篇
  6. 留后门?Nacos被爆存在严重的旁路身份验证安全漏洞!
  7. 可行性验证:API管理与ServiceMesh的集成
  8. 1 如何引用第三方滑动验证码
  9. Python SSL证书问题

随机推荐

  1. 【30篇突击 android】源码统计二
  2. Android ProgressDialog的使用
  3. 《Android程序运行过程,Android》
  4. Android资料
  5. Android SDK Manager无法自动更新
  6. ADT20新建项目Android Support library n
  7. mono for android 读取网络远程图片
  8. Android 删除手机联系人,添加手机联系人,更
  9. Android横竖屏切换
  10. ubuntu eclipse android搭建