Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I launch a new process as an impersonated user?

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
US
I am trying to launch a new process as a user with Administrative privileges using impersonation:

Code:
//A token for the Administrator previously obtained
//using LogonUser and DuplicateToken API calls
IntPtr ptrToken;
WindowsIdentity wid = new WindowsIdentity(ptrToken);
WindowsImpersonationContext imp = wid.Impersonate();

//This now returns the administrator name correctly
WindowsIdentity.GetCurrent().Name

Process prc = new Process();
prc.StartInfo.FileName = "msconfig";
prc.StartInfo.UseShellExecute = false;
prc.Start();

imp.Undo();

In the above code, the current process from the Task Manager retains the original username after impersonation begins. Also, when msconfig is launched, it does not have the Administrator username and it complains about not having Administrative privileges.

How do I start msconfig using a different username and password?

I have no trouble doing this if I use RunAs from the command prompt.

How do I do this from in my program?
 
The CreateProcessWithLogon API appears to be the solution. Please let me know if you know of a managed solution!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top