2009-07-22 103 views

回答

10

article解釋它很簡潔:

下面是文章的代碼片段:

IntPtr accessToken = IntPtr.Zero; 
.... 
//You have to initialize your accessToken with API calling 
.... 
WindowsIdentity identity = new WindowsIdentity(accessToken); 
WindowsImpersonationContext context = identity.Impersonate(); 
... 
// Now your code is using the new WindowsLogin and you can do what ever this login can do 
... 

//Now you can return to your current login of Windows 
context.Undo(); 
+2

文章中的代碼已不存在。 – 2016-12-02 14:58:42

20

也許最好是我迄今見過的最乾淨的code是這個

using (Impersonation.LogonUser(domain, username, password, logonType)) 
{ 
    // do whatever you want as this user. 
} 

只要按照GithubNuget

+1

這當然很容易實現,但我無法驗證它正在工作。我做了一個process.start(「cmd.exe」),並且該進程仍然顯示爲啓動該程序的ID所擁有的不是模擬的ID。我可能會錯過什麼? – 2016-10-27 18:09:37

相關問題