delphi – TLoginCredentialService用法示例

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

这个类的典型用例如何(源代码)?它是否在Delphi Datasnap / Web服务库中的某处使用?

解决方法

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

单击here以下载代码

在下面我将展示一些代码

首先,我需要一个记录来保存凭据,以及它们的列表:

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;

希望这能回答这个问题.别忘了下载完整的代码here

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...