2008-11-26 66 views
1

我正在嘗試執行以下操作: 1.我以管理員帳戶身份登錄到我的XP SP2操作系統,運行VS.NET 2005 2.本機還有另一個帳戶user1是一個來賓帳戶 3.我正在運行一個程序作爲管理員,從這個程序中我想啓動一個notepad.exe進程,它將在user1安全環境下運行 4.我特別想使用CreateProcessasUser來做到這一點。CreateProcessAsUser在我的實驗中無法正常工作

這是代碼剪斷,其中解釋了,我一直想什麼..

const string GRANTED_ALL = "10000000"; 

const int LOGON32_LOGON_INTERACTIVE = 2; 
const int LOGON32_LOGON_NETWORK = 3; 
const int LOGON32_LOGON_BATCH = 4; 
const int LOGON32_LOGON_SERVICE = 5; 
const int LOGON32_LOGON_UNLOCK = 7; 
const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
const int LOGON32_LOGON_NEW_CREDENTIALS = 9; 

const int LOGON32_PROVIDER_DEFAULT = 0; 
static IntPtr hToken = IntPtr.Zero; 
static IntPtr hTokenDuplicate = IntPtr.Zero; 

static void Main(string[] args) 
{ 
    int last_error = 0; 
    if(LogonUser("user1",null,"#welcome123", 
     LOGON32_LOGON_INTERACTIVE, 
     LOGON32_PROVIDER_DEFAULT, out hToken)) 
    { 
     last_error = Marshal.GetLastWin32Error(); 
     PROCESS_INFORMATION pi = new PROCESS_INFORMATION(); 
     STARTUPINFO si = new STARTUPINFO(); 
     SECURITY_ATTRIBUTES sa = new SECURITY_ATTRIBUTES(); 
     last_error = 0; 
     last_error = Marshal.GetLastWin32Error(); 
     if(DuplicateTokenEx(hToken,UInt32.Parse(GRANTED_ALL,System.Globalization.NumberStyles.HexNumber), 
      ref sa,SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, 
      TOKEN_TYPE.TokenPrimary,out hTokenDuplicate)) 
     { 
      last_error = 0; 
      last_error = Marshal.GetLastWin32Error(); 

      CreateProcessAsUser(hTokenDuplicate, "d:\\san\\notepad.exe", null, 
      ref sa, ref sa, false, 0, IntPtr.Zero, "d:\\san", ref si, out pi); 

      last_error = 0; 
      last_error = Marshal.GetLastWin32Error(); 

     } 
    } 

    last_error = 0; 
    last_error = Marshal.GetLastWin32Error(); 


    if (hToken != IntPtr.Zero) CloseHandle(hToken); 
    if (hTokenDuplicate != IntPtr.Zero) CloseHandle(hTokenDuplicate); 

} 

}

出於某種原因,這是行不通的.. 的DuplicateTokenEx函數返回的1305錯誤代碼和我似乎無法找出爲什麼..

相反DuplicateTokenEx的我還使用了DuplicateToken ,現在CreateProcessAsUser正在返回1308

錯誤代碼可能有人請扔在這個問題上的光。這似乎是一個顯然很簡單的事情,只是不能得到它的權利.. [請注意,我特別要先登錄用戶,然後重複CreateToken然後再CreateProcess AsUSer]

感謝 Santhosh

回答

-2

CreateProcessAsUser() windowstations and desktops

但我認爲這樣做在管理方式:

... 
using System.Diagnostics; 
using System.Security; 
... 
... 
string progPath = @"c:\WINNT\notepad.exe"; 
ProcessStartInfo startInfo = new ProcessStartInfo(progPath); 
startInfo.WindowStyle = ProcessWindowStyle.Normal; 
startInfo.UseShellExecute = false; 
startInfo.UserName = "SomeUser"; 
SecureString password = new SecureString(); 

#region setting password 
password.AppendChar('p'); 
password.AppendChar('a'); 
... 
#endregion 

startInfo.Password = password; 
Process.Start(startInfo); 
... 
...