I am having trouble invoking a batch file with impersonation (.NET 4.0)
I have a web application that invokes a test batch file residing on the local file system of the server. When I run it without impersonation it runs fine. But with impersonation the batch file doesnt produce any output, nor does it returns any error.
Here is the code for executing the batch file that i use -
public static bool ExecuteBatchFile(string fileLocationPath, string filePath, string arguments, TextBox textBox)
{
try
{
ProcessStartInfo procStartInfo = new ProcessStartInfo(filePath);
procStartInfo.UseShellExecute = false;
procStartInfo.Arguments = arguments;
procStartInfo.WorkingDirectory = fileLocationPath;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UserName = "XYZ";
procStartInfo.Domain = "ABC";
System.Security.SecureString pwd = new System.Security.SecureString();
foreach (char c in "PWD")
pwd.AppendChar(c);
procStartInfo.Password = pwd;
procStartInfo.LoadUserProfile = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
{
textBox.Text += "output rcvd\r\n";
textBox.Text += e.Data;
};
proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
{
textBox.Text += "error rcvd\r\n";
textBox.Text += e.Data;
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
proc.Close();
return true;
}
catch (Exception e)
{
textBox.Text += e.Message;
return false;
}
}
Without impersonation i can see the output of the batch file. I get Output received with output of batch file and then an empty OutputReceived, and then an empty ErrorReceived.
But with impersonation, i can see nothing! Just one event for OutputReceived with no data, and one event for ErrorReceived with no data.
I have set impersonation in the web config file as follows:
<identity impersonate="true" userName="ABC\XYZ" password="PWD"/>
I experienced similar problem with Windows 2003 Server where my service hosted in IIS was executing application.
Things i discovered:
1. You must ajust permissions on service and configure it to output stack trace (including folder where .Net puts built service).
2. Starting app with loading of user profile cannot be done from service. You can create wrapper application which you will execute without loading profile, and that app can in its turn execute app with loading of user profile. Wrapper must be built in Release mode and mustn't call Trace functions.
3. On service change you must
- restart application pool in IIS snapp-in
- restart site
- execute iisreset.
Related
protected void Button1_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process process1 = new System.Diagnostics.Process();
process1.StartInfo.FileName = Server.MapPath(#"~\\bin\\HelloApp.exe");
process1.StartInfo.Arguments = "";
process1.Start();
process1.WaitForExit();
process1.Close();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
try this code and see if you get any error. if you get an error check whether the published code bin has the HelloApp.exe.
protected void Button1_Click(object sender, EventArgs e)
{
string fileNameWithPath = Path.GetFullPath(#"~\\bin\\HelloApp.exe");
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = "";
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.FileName = fileNameWithPath;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
StreamReader outputReader = exeProcess.StandardOutput;
StreamReader errorReader = exeProcess.StandardError;
string errors = errorReader.ReadToEnd();
if (!string.IsNullOrEmpty(errors))
throw new Exception(errors);
}
}
It is impossible to call another UI application by the application hosted in IIS. Due to the fact that there is no user session to render the UI. Since Windows Vista, there is a mechanic called Session Isolation, which separates the application and the service.
However, it will work when we start the application by Visual Studio locally since the current session is inherited with the account running Visual Studio.
For details, please refer to the below links.
How to start a process from an IIS hosted WCF service?
IIS Session isolation problem
I have a WebAPI that works great but need to add the ability to call to an EXE on the server to run some tasks with Video, the server is our machine and running IIS to host the WebAPI.
I have tried it working with Process() and the calls make it to the cmd.exe file I have written but the issue is that the user is IUSER and this won't work as the Video processing needs to use system hardware so needs to be the current logged in Windows User.
I don't want to give the IUSER this privilege for obvious security reasons so I am looking for another way to call and pass data to the an EXE or background task (Short running <3seconds) and for that process to reply with the results.
It's all on the server which we have full control over.
Current Code:
string exeLoc = Environment.ExpandEnvironmentVariables(baseEXELocation);
using var process = new Process();
process.StartInfo.FileName = exeLoc;
process.StartInfo.Arguments = $"{command}";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, data) =>
{
if (!String.IsNullOrEmpty(data.Data))
{
Console.WriteLine(data.Data);
consoleData += data.Data;
}
};
process.Start();
process.BeginOutputReadLine();
var exited = process.WaitForExit(1000 * 10); // (optional) wait up to 10 seconds
Thanks
You could try to use the impersonation in iis:
<identity impersonate="true" />
https://support.microsoft.com/en-us/help/306158/how-to-implement-impersonation-in-an-asp-net-application
or assign the administrator permission to the application pool by using application pool advance setting -> identity to the custom account.
or You can try using the verb runas in Process.Start to execute the exe file as an Administrator.
ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas"; //this is how you pass this verb
I am trying to print a pdf file from a WebAPI Web Service by calling GhostScript using Process()
This works perfectly when 'Debugging' an ASP.NET application on my local machine, but when I use the exact same code to try and print through a Web Service - also on my local machine - it doesn't work.
I don't get any kind of exception in VS - so far as the application is concerned, the application is working fine.
Here's the code I'm using to call the GhostScript printer:
Process process = new Process();
process.StartInfo.Arguments = #" -dPrinted -dBATCH -dNOPROMPT -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -dNoCancel -sPAPERSIZE=a4 -sOutputFile=\\spool\\\printserver\printer c:\\test\test.pdf";
process.StartInfo.FileName = #"C:\Program Files (x86)\GPLGS\gswin32c.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) => {
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
Console.Write(e.Data);
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
Console.Write(e.Data);
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.HasExited == false) process.Kill();
return process.ExitCode == 0;
I thought this might have something to do with printers not being installed for All Users so have added them for all users. The only other thing that I can see it being is Permissions - but can't figure out a solution.
Does anyone have any ideas?
Thanks in advance for your help. :)
As suspected, this turned out to be a Permissions issue. After publishing the Web Service to IIS, I changed the Default Application Pool to use a User Identity, the printing suddenly started working correctly.
I will set up a user account specifically for printing - with no write permissions - and use this for now.
I'm creating a service on VS2010, using .net framework 4.0 Client Profile. The target machine is Windows Server 2003 64 bits. This service move some files and then executes a process with System.Diagnostics.Process. The trouble is that, even if the taskmanager shows a process as starting, the executable never do whats was made for. Example code:
private void imprimir(string nombreImpresora, int copias, string nombreArchivo)
{
try
{
string copiasSumatra = "1,";
for (int i = 1; i < copias; i++)
{
copiasSumatra += "1,";
}
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string comando = String.Format("-print-to \"{0}\" \"{1}\" -print-settings \"{2}odd,fit\" -silent", nombreImpresora, nombreArchivo, copiasSumatra);
string filename = '"' + Path.Combine(path, "SumatraPDF.exe") + '"';
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.WorkingDirectory = path;
proc.StartInfo.FileName = filename;
proc.StartInfo.Arguments = comando;
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.ErrorDialog = false;
proc.Start();
proc.WaitForExit();
lc.writeToLog("Instruction executed. Exit code: " + proc.ExitCode);
}
catch (Exception ex)
{
lc.writeToLog(ex.Message + " " + ex.StackTrace);
}
}
If I execute it on my dev machine (windows 8 pro) or in another test server (Windows Server 2003 32 bits) it makes whats expected. If I run it on the WS2003 64 bit server it does nothing.
I've debugged lots of times to see if it produces some error that I'm missing, but nothing happens. The "lc.writeToLog" method prints text to a file. I've used it to log every single line of the execution, but no error is thrown. Using that method I've concluded that it passes the "proc.WaitForExit()" instruction, so I think it's going to do what I've programmed, but nothing happens.
I have runned the same instruction but passing it a user, password and domain and the result was the same. Also tryed to capture standard error and output but it contained nothing.
What could be the trouble?
It was a server related issue. After deploying the application onto the production server the issue has disapeared.
I'm developing an Windows Forms application that requires me to call a separate program to perform a task. The program is a console application and I need to redirect standard output from the console to a TextBox in my program.
I have no problem executing the program from my application, but I don't know how to redirect the output to my application. I need to capture output while the program is running using events.
The console program isn't meant to stop running until my application stops and the text changes constantly at random intervals. What I'm attempting to do is simply hook output from the console to trigger an event handler which can then be used to update the TextBox.
I am using C# to code the program and using the .NET framework for development. The original application is not a .NET program.
EDIT:
Here's example code of what I'm trying to do. In my final app, I'll replace Console.WriteLine with code to update the TextBox. I tried to set a breakpoint in my event handler, and it isn't even reached.
void Method()
{
var p = new Process();
var path = #"C:\ConsoleApp.exe";
p.StartInfo.FileName = path;
p.StartInfo.UseShellExecute = false;
p.OutputDataReceived += p_OutputDataReceived;
p.Start();
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(">>> {0}", e.Data);
}
This works for me:
void RunWithRedirect(string cmdPath)
{
var proc = new Process();
proc.StartInfo.FileName = cmdPath;
// set up output redirection
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.EnableRaisingEvents = true;
proc.StartInfo.CreateNoWindow = true;
// see below for output handler
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
// output will be in string e.Data
}
You can use the following code
MemoryStream mem = new MemoryStream(1000);
StreamWriter writer = new StreamWriter(mem);
Console.SetOut(writer);
Assembly assembly = Assembly.LoadFrom(#"C:\ConsoleApp.exe");
assembly.EntryPoint.Invoke(null, null);
writer.Close();
string s = Encoding.Default.GetString(mem.ToArray());
mem.Close();
I've added a number of helper methods to the O2 Platform (Open Source project) which allow you easily script an interaction with another process via the console output and input (see http://code.google.com/p/o2platform/source/browse/trunk/O2_Scripts/APIs/Windows/CmdExe/CmdExeAPI.cs)
Also useful for you might be the API that allows the viewing of the console output of the current process (in an existing control or popup window). See this blog post for more details: http://o2platform.wordpress.com/2011/11/26/api_consoleout-cs-inprocess-capture-of-the-console-output/ (this blog also contains details of how to consume the console output of new processes)
Thanks to Marc Maxham for his answer that save me time !
As Jon of All Trades notice it, UseShellExecute must be set to false in order to redirect IO streams, otherwise the Start() call throws an InvalidOperationException.
Here is my modification of the code where txtOut is a WPF readonly Textbox
void RunWithRedirect(string cmdargs)
{
var proc = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe", "/k " + cmdargs)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
// see below for output handler
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Dispatcher.BeginInvoke(new Action( () => txtOut.Text += (Environment.NewLine + e.Data) ));
}