I\'m trying to start an elevated process from with a non-elevated process, but I also need to supply the username and password for a user with administrative credentials. I\'ve
If you're authoring a Windows Installer (MSI) application, and updating it using MSPs, then Windows Installer has built-in support for exactly your scenario: - check out User Account Control (UAC) Patching.
It works basically like this:
According to the MSDN documentation:
When UseShellExecute is false, you can start only executables by using the Process object.
I noticed your declaration var proc = Process.Start(info);
is not using Process
as the class type.
Also make sure parameter path
is the fully qualified path to the executable. For instance, "c:\\directory\\contains\\process_to_be_started\\executable.exe"
According to the MSDN documentation this is important:
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
I would try below code to run target process with elevated privileges (with admin rights).
ProcessStartInfo info = new ProcessStartInfo(path);
info.UseShellExecute = false;
info.UserName = username;
info.Password = securePwd;
info.Domain = "MyDomain";
info.Verb = "runas";
info.WorkingDirectory = "c:\\directory\\contains\\process_to_be_started"
'var proc = Process.Start(info);
Process proc = Process.Start(info);
i was surprised there's no way to do this, until i found an on blog entry by Chris Jackson:
Why Can’t I Elevate My Application to Run As Administrator While Using CreateProcessWithLogonW?
You need a bootstrapper. Some process which will let you do the transition to the alternate user, which could be responsible for running the requireAdministrator application. So, you could design something like this:
Why don’t we just create the
ShellExecuteWithLogonW
API? I’ll never say never, and we might at some point. But today, the use cases for this APIs have been use cases where there has been an alternate design which is superior.The most common request is for people writing home-grown software deployment software, where they’d like to encode credentials right into the app and elevate their own process. The real problem here is not the lack of the API, it’s that you have admin credentials encoded in your app for the world to read.
So the solution requires ShellExecute
, it's the only one that knows how to trigger a Consent dialog.
It brings up a good point: What are you doing with a person's password already?
There's no UAC on Server Core because there's no windows to show a consent prompt.
The ProcessStartInfo.Verb="runas"
is for only windows Vista and higher, so you should ask for the system level, and not do the elevation for XP.
I think if you choose ProcessStartInfo.Verb="runas"
, you should not specify user name and password.
If UAC is of, then it suppose to succeed anyway, it shouldn't be a problem.
From MSDN:
You cannot elevate an already running process. Thus, you should refactor your app to be separated into admin & non-admin operations - running the default application with normal privileges and starting another elevated process for each administrative operation.
Let's work with that, assuming you request administrator rights from the outset on the processes that require them. Based upon the context you've provided:
The issue seems to be setting
UseShellExecute
tofalse
(as both approaches work fine when this is not the case), but I have to set it to false in order to launch the process under a different user account.
As you mentioned, exactly as noted in the documentation for UseShellExecute:
UseShellExecute must be false if the UserName property is not Nothing or an empty string, or an InvalidOperationException will be thrown when the Process.Start(ProcessStartInfo) method is called.
We now know you're executing your program directly instead of through the use of a shell. This is valuable information.
Backpathing through the documentation, the docs for ProcessStartInfo carry the following security note:
This class contains a link demand at the class level that applies to all members. A SecurityException is thrown when the immediate caller does not have full-trust permission. For details about security demands, see Link Demands.
So, you don't have the right Link Demand. While trying to solve your permissions issue, you inadvertently created another permissions issue.
The upshot is you need to decorate your calling method with the right Security Demand, which should be FullTrust
. You can do this declaratively or imperatively within your code.
(Additional reading)