Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Code as a different user

Is there a way to tell my code to run as a different user?

I am calling NetUserSetInfo via a PInvoke and I need to call it as a different user. Is there a way to do that?

like image 204
Vaccano Avatar asked Jul 22 '09 22:07

Vaccano


People also ask

How do I run a program as a different user?

Use Run As to start a program as another userLocate the program that you want to start in Windows Explorer, the Microsoft Management Console (MMC), or Control Panel. Press and hold down the SHIFT key while you right-click the .exe file or icon for the program, and then click Run as. Click The following user.

How do I impersonate a user in C#?

To impersonate the IIS authenticating user on every request for every page in an ASP.NET application, we must include an <identity> tag in the Web. config file of this application and set the impersonate attribute to true.


1 Answers

Impersonation requires calling some native APIs (namely, LogonUser) so it's probably not worth posting 3 pages of wrapper code. This page has a complete working sample: http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/

Note that impersonation has important security considerations. Make sure you follow best practices.

like image 195
Richard Berg Avatar answered Oct 05 '22 16:10

Richard Berg