这个新类的
documentation page – 在XE2中引入 – 仅包含对TObject文档或占位符的引用.我可以看到这个类提供了一个RegisterLoginHandler方法,以及一个使用
TLoginCredentialEvent类的UnRegisterLoginHandler方法.这使用带有用户名和密码的
TLoginEvent对象.
这个类的典型用例如何(源代码)?它是否在Delphi Datasnap / Web服务库中的某处使用?
解决方法
我刚刚创建了一个如何使用它的小型演示
在下面我将展示一些代码:
首先,我需要一个记录来保存凭据,以及它们的列表:
Type TCredential = record Username,Password,Domain: string; constructor Create(const aUsername,aPassword,aDomain: string); function AreEqual(const aUsername,aDomain: string): Boolean; end; TCredentialList = class(TList<TCredential>) public function IsValidCredential(const aUsername,aDomain: string): Boolean; end;
然后我们需要定义一个我们称之为上下文的上下文.这只是一个应用程序唯一字符串,用于识别每个登录功能
const Context = 'TForm1';
在表单创建中,我创建了我的列表并向其添加虚拟数据
procedure TForm1.FormCreate(Sender: TObject); begin CredentialList := TCredentialList.Create; //Add Dummy data CredentialList.Add(TCredential.Create('AA','AA','DomainAA')); CredentialList.Add(TCredential.Create('BB','BB','DomainAA')); CredentialList.Add(TCredential.Create('CC','CC','DomainAA')); // Register your Login handler in a context. // This method is called when you try to login // by caling TLoginCredentialService.GetLoginCredentials(); TLoginCredentialService.RegisterLoginHandler(Context,LoginCredentialEvent); end;
procedure TForm1.Button1Click(Sender: TObject); begin // The actual call to login // First param is the context // Second Parameres is a callback function given to the event handler. TLoginCredentialService.GetLoginCredentials(Context,function { LoginFunc } (const Username,Domain: string): Boolean begin //The actual user validation Result := CredentialList.IsValidCredential(Username,Domain); end); end;
最后我只需要实现我的loginhandler:
//This is the "onLogin" event handler. //This is called durring a login attempt //The purpose of this event handler are to call tha callBack function with correct information //and handle the result procedure TForm1.LoginCredentialEvent(Sender: TObject; Callback: TLoginCredentialService.TLoginEvent; var Success: Boolean); begin //Call the callback Callback(Sender,LabeledEdit1.Text,LabeledEdit2.Text,LabeledEdit3.Text,Success); //Handle the success. if Success then Label1.Caption := 'Yes' else Label1.Caption := 'No'; end;
原文地址:https://www.jb51.cc/delphi/102120.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。