这个新类的文档页面 - 在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, aPassword, aDomain: string): Boolean;
end;
TCredentialList = class(TList<TCredential>)
public
function IsValidCredential(const aUsername, aPassword, aDomain: string): Boolean;
end;
Run Code Online (Sandbox Code Playgroud)
那么我们需要定义一个上下文,我们称之为上下文。多数民众赞成只是一个应用程序唯一的字符串,该字符串标识每个LoginFunction
const
Context = 'TForm1';
Run Code Online (Sandbox Code Playgroud)
在Form create中,我创建我的列表并向其中添加虚拟数据
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;
Run Code Online (Sandbox Code Playgroud)
从拨打电话到登录,我都在表单上放置了一个按钮:
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, Password, Domain: string): Boolean
begin
//The actual user validation
Result := CredentialList.IsValidCredential(Username, Password, Domain);
end);
end;
Run Code Online (Sandbox Code Playgroud)
最后,我只需要实现我的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;
Run Code Online (Sandbox Code Playgroud)
希望这能回答问题。不要忘记在此处下载完整的代码
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |