gpt4 book ai didi

delphi - TLoginCredentialService 使用示例

转载 作者:行者123 更新时间:2023-12-03 14:44:30 24 4
gpt4 key购买 nike

documentation page这个新类的内容(在 XE2 中引入)仅包含对 TObject 文档或占位符的引用。我可以看到这个类提供了一个 RegisterLoginHandler 方法和一个 UnRegisterLoginHandler 方法,它们使用 TLoginCredentialEvent类(class)。这使用 TLoginEvent带有用户名和密码的对象。

此类的典型用例是什么样的(源代码)?它是否在 Delphi Datasnap/Web 服务库中的某个地方使用?

最佳答案

我刚刚创建了一个关于如何使用它的小演示

点击here下载代码

下面我将展示一些代码:

首先,我需要一个记录来保存凭据及其列表:

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;

然后我们需要定义一个上下文,我们在其中调用它。这只是一个应用程序唯一的字符串,用于标识每个登录函数

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, Password, Domain: string): Boolean
begin
//The actual user validation
Result := CredentialList.IsValidCredential(Username, Password, Domain);
end);
end;

最后我只需要实现我的登录处理程序:

//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;

希望这能回答问题。不要忘记下载完整的代码 here

关于delphi - TLoginCredentialService 使用示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12411496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com