2009-07-22 100 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

相关问题