How To…Thread-safe Logon Validation

Question

“How can I perform thread-safe logon validation? I have defined a Server.SessionParameters for UserName and Password and am using the Server.OnValidateSessionParameters event.

Solution

The Server is a singleton. This example shows how use a Delphi TCriticalSection object to ensure thread safety.

Download: CustomSeverLogging.zip

Sample Delphi code:

uses
  SyncObjs;


{TForm1.FormCreate}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FLock := TCriticalSection.Create;

end;

{TForm1.FormDestroy}

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FLock.Free;

end;

{TForm1.rsServer1ValidateSessionParameters}

procedure TForm1.rsServer1ValidateSessionParameters(Sender: TObject; aParameters: TppParameterList; var aIsValid: Boolean);
begin

  FLock.Acquire;

  try
    {validate the parameter names}
    aIsValid := (aParameters.InList('UserName') and aParameters.InList('Password'));

    if aIsValid then
      aIsValid := PerformLoginValidation(aParameters['UserName'].Value, aParameters['Password'].Value);

  finally
    FLock.Release;
  end;

end;

{TForm1.PerformLoginValidation}

function TForm1.PerformLoginValidation(const aUserName, aPassword: string): Boolean;
begin
  Result := False;

  // add database validation logic here

end;