2009-07-22 5 views
30

내 사용자가 다른 사용자로 실행되도록 지시하는 방법이 있습니까?다른 사용자 (C#)로 코드 실행

PInvoke를 통해 NetUserSetInfo를 호출 중이므로이를 다른 사용자로 호출해야합니다. 그렇게 할 수있는 방법이 있습니까? 여기

기사에서 코드입니다 :

답변

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

기사의 코드가 사라졌습니다. –

20

은 아마 최고의 내가 지금까지 본 가장 깨끗한 code 그냥 Github 또는 Nuget에 따라이

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

입니다.

+1

이것은 구현하기가 쉬웠지만 제대로 작동하는지 확인할 수 없습니다. 나는 process.start ("cmd.exe") 프로세스를 수행하고 프로세스는 여전히 가장 된 ID가 아닌 프로그램을 시작한 ID가 소유하고 있음을 보여줍니다. 나는 무엇을 놓칠 수 있 었는가? –