简体   繁体   中英

C# logged user and run as user

I have 2 users: domain1\\user1 and domain2\\user2. I'm logged with domain1\\user1. I want to run app (with option 'Run as different user') with domain2\\user2. How can I gets the user name of the person who is currently logged on to the Windows, because Environment.UserName return user2, but I want domain1\\user1.

Check out WindowsImpersonationContext on MSDN, I believe it has what you're looking for.

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx

You can implement your own helper class if you like, then declare using([establish your context here]) and perform your activities within that code block that will be executed with that user context. Quite handy!

Note: If you decide to utilize using , be sure in the Dispose() method of your helper that you release the impersonation context so that everything cleans up nicely when your program leaves the using block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM