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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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; |