Navigation:  Advantage Developer's Guide > Part III - Accessing Advantage Data > Chapter 15 - Using Advantage from Delphi >

     Changing a User Password

Advantage Database Server v8.1: A Developer’s Guide

by Cary Jensen and Loy Anderson

  © 2007 Cary Jensen and Loy Anderson. All rights reserved.

Previous pageReturn to chapter overviewNext page

A user can change the password on the user’s own connection, if you permit this change. In most cases, only when every user has a distinct user name would you expose this functionality in a client application. When multiple users share a user name, this operation is usually reserved for an application administrator.

As was done in the preceding section, this code demonstrates changing a user password using an AdsDictionary component. For an example of changing a password using the sp_ModifyUserProperty stored procedure, refer to Chapters 16–18.

procedure TForm1.ChangePasswordBtnClick(Sender: TObject);
 UserName: String;
 OldPass: String;
 NewPass1: String;
 NewPass2: String;
 function CheckPass(UName, UPass: String): Boolean;
   TempConnection: TAdsConnection;
   result := False;
   TempConnection := TAdsConnection.Create(nil);
     TempConnection.AliasName := AdsConnection1.AliasName;
     TempConnection.AdsServerTypes :=
     TempConnection.Username := UName;
     TempConnection.Password := UPass;
     TempConnection.LoginPrompt := False;
       TempConnection.IsConnected := True;
       result := False;
     result := True;
      TempConnection.IsConnected := False;
 end; //CheckPass
 UserName := AdsConnection1.Username;
 OldPass := 'Enter password';
 OldPass := InputBox('Password',
 'Enter your current password', OldPass);
 if OldPass = 'Enter password' then Exit;
 if not CheckPass(UserName, OldPass) then
   ShowMessage('Cannot validate your current password. ' +
     'Cannot change password');
 NewPass1 := '';
 NewPass2 := '';
 NewPass1 := InputBox('Password',
   'Enter your new Password', NewPass1);
 if NewPass1 = '' then
   ShowMessage('Password cannot be blank. ' +
     'Cannot change password');
 NewPass2 := InputBox('Password',
   'Confirm your new password', NewPass2);
 if NewPass1 <> NewPass2 then
   ShowMessage('Passwords did not match. ' +
     'Cannot change password');
 //Connect AdsDictionary1 and change password
 AdsDictionary1.AliasName := AdsConnection1.AliasName;
 AdsDictionary1.AdsServerTypes :=
 AdsDictionary1.UserName := AdsConnection1.Username;
 AdsDictionary1.Password := OldPass;
 AdsDictionary1.LoginPrompt := False;
   (StrLen(PChar(NewPass1)) + 1));
 ShowMessage('Password successfully changed. ' +
   'New password will be valid next time you connect');

This code segment is a bit simpler than the preceding one. After verifying that the user knows the current password with which they are connected, they are prompted for their new password twice, for confirmation purposes. Next, the AdsDictionary component is configured to connect with the user account, after which its SetUserProperty method is invoked to change the user's password. As the final dialog box displayed by this event handler indicates, this password will be valid once the user terminates all connections on this user account.

NOTE: If you run this code, and change the password of the adsuser account, you should use the Advantage Data Architect to change the password back to password. Otherwise, you will not be able to run this project again, since the literal 'password' is assigned to the Password property of the AdsConnection component in this project.