Programmatic login by providing credentials

Consider users of Windows A (with administrator rights) and B (limited access rights). Also, a data folder located on the server, to which only user A has access.

Im's task is to register windows through user B and through a Delphi application trying to access the data folder using the credentials of user A programmatically.

Is there an API function that would allow me to achieve this goal?

+4
source share
1 answer

You can impersonate a registered user to access the data folder using LogonUser , ImpersonateLoggedOnUser and RevertToSelf .

Try this sample

 {$APPTYPE CONSOLE} uses Windows, SysUtils; function ConnectAs(const lpszUsername, lpszPassword: string): Boolean; var hToken : THandle; begin Result := LogonUser(PChar(lpszUsername), nil, PChar(lpszPassword), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken); if Result then Result := ImpersonateLoggedOnUser(hToken) else RaiseLastOSError; end; begin try ConnectAs('Admin','Password'); //do something here //terminates the impersonation RevertToSelf; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; readln; end. 
+5
source

Source: https://habr.com/ru/post/1485797/


All Articles