如何動態切換Windows使用者身份? |
尚未結案
|
kensoong
初階會員 ![]() ![]() 發表:31 回覆:70 積分:45 註冊:2003-05-28 發送簡訊給我 |
|
rexchiu
中階會員 ![]() ![]() ![]() 發表:14 回覆:88 積分:70 註冊:2002-03-17 發送簡訊給我 |
請依照順序使用以下三個API
1.BOOL RevertToSelf(void);
2.BOOL LogonUser(
LPTSTR lpszUsername,
LPTSTR lpszDomain,
LPTSTR lpszPassword,
DWORD dwLogonType,
DWORD dwLogonProvider,
PHANDLE phToken
);
3.BOOL ImpersonateLoggedOnUser(
HANDLE hToken
);
等你做完你的動作後,在一次呼叫RevertToSelf();即可.
此三個windows api 之詳細用法請參閱相關文件. Best Regards,
Rex Chiu
------
Best Regards, Rex Chiu |
kensoong
初階會員 ![]() ![]() 發表:31 回覆:70 積分:45 註冊:2003-05-28 發送簡訊給我 |
感謝~
DLL是Advapi32.dll 附上asp範例...
Set Advapi = CreateObject("ScriptUtils.Advapi") Response.Write Advapi.UserName
Advapi.ImpersonateLoggedOnUser (Advapi.LogonUser("aa", ""))
Response.Write Advapi.UserName
Advapi.RevertToSelf Response.Write Advapi.UserName 答案不全,自己先附上Delphi範例:
unit Logon; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
jpeg, ExtCtrls, StdCtrls, ComCtrls; type
TfrLogon = class(TForm)
UserNameTxt: TEdit;
PasswordTxt: TEdit;
btnOk: TButton;
btnCancel: TButton;
GroupBox1: TGroupBox;
lblUserName: TLabel;
lblPassword: TLabel;
Panel1: TPanel;
Panel2: TPanel;
btnShutdown: TButton;
procedure btnCancelClick(Sender: TObject);
procedure UserNameTxtExit(Sender: TObject);
procedure PasswordTxtExit(Sender: TObject);
procedure btnOkClick(Sender: TObject);
procedure btnShutdownClick(Sender: TObject);
private
{ Private declarations }
function Login(Username, Password :String): integer;
procedure WinExecAsUser(FileName: string; username: string; password:string);
public
{ Public declarations }
function ShowLogon: integer;
function ExitWindowsEx (dwReserved: DWORD; code: Word): Bool;
end; var
frLogon: TfrLogon;
LogonResult: integer; implementation {$R *.DFM} function TfrLogon.ShowLogon: integer;
begin
if frLogon = nil then
frLogon := TfrLogon.Create(self);
try
frLogon.ShowModal;
finally
Result := LogonResult;
frLogon.Free;
end;
end; function TfrLogon.Login(Username,Password :String): integer;
var
UserToken : Cardinal;
Ret : Bool;
//zAppName : array[0..512] of char;
//StartupInfo : TStartupInfo;
//ProcessInfo : TProcessInformation;
//Visibility : integer;
//fRet : Bool;
//hPrimaryToken : Cardinal;
a : integer; begin ShowMessage('Before LogonUser');
Ret := LogonUser(PChar(Username), '', PChar(Password),LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT, UserToken);
ShowMessage('After LogonUser'); if (Ret <> False) then
begin
ShowMessage('Before ImpersonateLoggedOnUser');
ImpersonateLoggedOnUser(UserToken);
ShowMessage('After ImpersonateLoggedOnUser');
end
else
begin
a:= GetLastError();
ShowMessage(IntToStr(a));
Result := a;
end; { StrPCopy(zAppName, 'Userinit.exe');
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility; if not LogonUser(PChar(Username), '', PChar(Password),LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT, UserToken) then
ShowMessage(SysErrorMessage(GetLastError)); //hPrimaryToken := nil; fRet := DuplicateTokenEx(UserToken, MAXIMUM_ALLOWED, nil,
SecurityImpersonation, TokenPrimary, hPrimaryToken); if not fRet then
begin
ShowMessage(SysErrorMessage(GetLastError));
end; ShowMessage('before create process as user');
CreateProcessAsUser(UserToken,nil,
zAppName, { pointer to command line string }
// nil, { pointer to process security attributes }
// nil, { pointer to thread security attributes }
// false, { handle inheritance flag }
// CREATE_NEW_CONSOLE or { creation flags }
// NORMAL_PRIORITY_CLASS,
// nil, { pointer to new environment block }
// nil, { pointer to current directory name }
// StartupInfo, { pointer to STARTUPINFO }
// ProcessInfo); { pointer to PROCESS_INF }
// ShowMessage('after create process as user'); { if ProcessInfo.hProcess <> 0 then
begin
CloseHandle(ProcessInfo.hProcess);
end;
if ProcessInfo.hThread <> 0 then
begin
CloseHandle(ProcessInfo.hThread);
end; } {if GetLastError <> 0 then
begin
ShowMessage(SysErrorMessage(GetLastError));
end; } end; procedure TfrLogon.WinExecAsUser(FileName: string; username: string; password:
string);
var
zAppName : array[0..512] of char;
StartupInfo : TStartupInfo;
ProcessInfo : TProcessInformation;
UserToken : thandle;
Visibility : Integer;
begin
StrPCopy(zAppName, FileName);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility; if not LogonUser(PChar(Username), '', PChar(Password),
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT, UserToken) then
ShowMessage(SysErrorMessage(GetLastError)); CreateProcessAsUser(UserToken, nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo); { pointer to PROCESS_INF } if GetLastError <> 0 then
ShowMessage(SysErrorMessage(GetLastError));
end; function TfrLogon.ExitWindowsEx (dwReserved: DWORD; code: Word): Bool;
var
hdlProcessHandle : Cardinal;
hdlTokenHandle : Cardinal;
tmpLuid : Int64;
tkp : TOKEN_PRIVILEGES;
tkpNewButIgnored : TOKEN_PRIVILEGES;
lBufferNeeded : Cardinal;
Privilege : array[0..0] of _LUID_AND_ATTRIBUTES; begin
hdlProcessHandle := GetCurrentProcess;
OpenProcessToken(hdlProcessHandle,
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),
hdlTokenHandle); // Get the LUID for shutdown privilege.
LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);
Privilege[0].Luid := tmpLuid;
Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0] := Privilege[0];
AdjustTokenPrivileges(hdlTokenHandle,
False,
tkp,
Sizeof(tkpNewButIgnored),
tkpNewButIgnored,
lBufferNeeded); end; procedure TfrLogon.UserNameTxtExit(Sender: TObject);
begin
if UserNameTxt.Text = '' then
begin
UserNameTxt.SetFocus;
MessageDlg('You must enter username to continue', mtError, [mbok],0);
end;
end; procedure TfrLogon.PasswordTxtExit(Sender: TObject);
begin
if PasswordTxt.Text = '' then
begin
PasswordTxt.SetFocus;
MessageDlg('You must enter password to continue', mtError, [mbok],0);
end;
end; procedure TfrLogon.btnOkClick(Sender: TObject);
begin
LogonResult := Login(UserNameTxt.Text, PasswordTxt.Text);
WinExecAsUser('c:\WINNT\system32\userunit.exe', UserNameTxt.Text, PasswordTxt.Text);
end; procedure TfrLogon.btnCancelClick(Sender: TObject);
begin
UserNameTxt.Text := '';
PasswordTxt.Text := '';
end; procedure TfrLogon.btnShutdownClick(Sender: TObject);
begin
ExitWindowsEx(EWX_SHUTDOWN, 0);
end; end. 發表人 - kensoong 於 2004/04/25 09:15:49 發表人 - kensoong 於 2004/04/25 09:33:58
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |