Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TLoginCredentialService usage example

The documentation page of this new class - introduced in XE2 - only contains references to the TObject documentation, or placeholders. I can see that this class offers a RegisterLoginHandler method, and a UnRegisterLoginHandler method, which use a TLoginCredentialEvent class. And this uses a TLoginEvent object with username and password.

How would the typical use case of this class look like (source code)? Is it used somewhere in the Delphi Datasnap/Web services libraries?

like image 829
mjn Avatar asked Sep 13 '12 17:09

mjn


1 Answers

I've just created a small demo of how to use it

Click here to download the code

In the following I'll show some of the code:

First I need a record to hold Credentials, and a list of them :

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;

then we need to define a context in wich we are calling this. Thats just a application unique string wich identifyes each Loginfunction

const
  Context = 'TForm1';

In Form create I create my list and add dummy data to it

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;

I have placed a button on my form from wich I make my call to login :

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;

Finally I just need to implement my 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;

Hope this answers the question.Dont forget to download the complete code here

like image 199
Jens Borrisholt Avatar answered Oct 06 '22 04:10

Jens Borrisholt