I know this question was asked before over and over, but I couldn´t find a solution that worked for me.
my application needs to do some modification in the program files folders. It starts as a regular user. I found out that adding the app.manifest helps. So here´s part of my code:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.Arguments = #"/c echo abc > ""%programfiles(x86)%\testfile.txt"""
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.WorkingDirectory = #"C:\Temp";
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.WriteLine(proc.StandardError.ReadToEnd());
proc.WaitForExit();
In my app.manifest this is what is relevant:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
If the user executing the application is a regular user, a UAC prompt will pop up requesting username and password (because of the manifest). Now, if I start the application with an admin user (using psexec for example)...
psexec.exe -u Username -p Password cmd /c myapp.exe
... I thought it would run like a charm, but instead it pops up a slightly different message asking me to confirm that I want the program to run (without fields for username and password). If I say yes, it runs fine.
What I want is that it runs with admin privileges, without any prompt, and without disabling UAC on registry for example. Is that even possible?
Related
I have to design an API which will first pull the latest code from the repository and then execute some node js command on that code. I execute this using command prompt through c# code.
Code sample:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.WorkingDirectory = Path;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c git pull";
p.Start();
p.WaitForExit();
p.StartInfo.Arguments = "/c node populate";
p.Start();
Everything work perfect on my local machine. But when I deployed this on the server only the git pull command not working, rest other commands working perfectly.
Even another git command like - git status and git rev-parse --short HEAD do work correctly but the problem seems with git pull.
I also tried git pull https://user:pass#domain/repo but no luck.
When I execute git pull command on the server manually through command prompt it works.
If it is due to IIS user permission than what setting are required.
Any guidance would be highly appreciated.
I am attempting to start a hidden console application, but require the application to have elevated privileges.
I have successfully managed to have the UAC prompt appear, however can't get it to appear when I try to start the process hidden.
ProcessStartInfo proc = new ProcessStartInfo();
proc.UseShellExecute = false;
proc.FileName = "C:/example.exe";
proc.CreateNoWindow = true;
proc.Verb = "runas";
This will result in a process starting in the background, except without a prompt appearing (and without elevated priveleges). If I change UseShellExecute to true, then the UAC prompt appears, however the console window is visible too.
You need to set WindowStyle to ProcessWindowStyle.Hidden, as Jexus Manager shows,
https://github.com/jexuswebserver/JexusManager/blob/be90688abd03780a714dc401054e22fa4afa2be3/Microsoft.Web.Administration/IisExpressServerManager.cs#L41
I am trying to run a jar in C# by running this code:
System.Diagnostics.ProcessStartInfo processInfo = new System.Diagnostics.ProcessStartInfo(pathForjre+"java.exe", "-jar "+jar+" "+argsforjar);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = processInfo;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = false;
string out = "";
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
out += proc.StandardOutput.ReadLine();
proc.Close();
return out;
When the jar runs I get an access denied exception with the path the jar is trying to write the log file to. I can manually run the jar from the command line and the log file writes no problem. How do I give the process permission to do things like write a file?
ASP.NET apps by default don't impersonate the identity of the user that is hitting the site. Typically then run as the local Network Service account unless you specifically have the process run under a different identity (you can determine the identity it's using by looking at Environment.UserName).
You can either set up the site (or use impersonation) to run under an identity that has permission to write to that location, or give write permision to the account that it's currently using.
Setting the working directory of the process resolved the issue.
proc.StartInfo.WorkingDirectory = path;
I have a service with WCF in a WPF application (self-hosted) and I have the typical error "Your process does not have access rights to this namespace".
The users can’t have admin privileges so using a .manifest is not a solution.
The ports are dynamic, the application calculate a free port every time is running, so the application must insert the listen port by netsh several times
I use a ProcessStartInfo with domain administrator, but to start the process the user needs admin privileges.
Run the application as administrator neither is a solution, so I need that a normal user can run the application and the program add the port by netsh as domain administrator.
My Process is something like this:
ProcessStartInfo psi = new ProcessStartInfo("netsh", parameter);
SecureString ss = new SecureString();
for (int i = 0; i < adminPass.Length; i++)
ss.AppendChar(adminPass[i]);
psi.Password = ss;
psi.UserName = Admin;
psi.Domain = Domain;
psi.Verb = "runas";
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process.Start(psi);
Thanks a lot
Take a look at the accepted answer for this Stack Overflow question for a possible solution to your problem. The approach outlined in the answer is to break out the admin-requiring code out into a Windows service which performs the elevated privilege operations under an appropriate (separate) account when invoked.
I am trying to launch csript as an administrator (the account logged in has admin rights). setting the startinfo.verb to runas does not work.
ProcessStartInfo p1 = new ProcessStartInfo();
p1.UseShellExecute = true;
p1.Verb = "runas";
p1.FileName = "cscript";
p1.Arguments = "I:\\WPKG\\wpkg.js /synchronize /quiet /nonotify";
Process p = new Process();
p.StartInfo = p1;
p.Start();
The only way I can get it to start with privileges is to manually set the username and password. However I cannot hardcode that information or put it into configurations. Is there any way to have the cmd elevate without the login info?
I have also tried adding using (System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate()) around the above code with no luck either.
Note: If I run the bat file directly, it works, if i run with password hardcoded, it works. It only fails to elevate launching from C# without login information.
This utility:
http://jpassing.com/2007/12/08/launch-elevated-processes-from-the-command-line/
may help. Note too the comments in that post.
You have several choices:
Use the runas utility (not verb), and pass in the username and password to . You may need to do this manually once with saving the credentials (only good till a restart).
Pass in the username and password on the ProcessStartInfo (you will need to convert the password to a SecureString.
Use the runas verb (not utility) to interact with UAC, as described in this question.