2

I have the following code which is working. My callback method is called with the program's output as it is generated.

var proc = new System.Diagnostics.Process();
//proc.StartInfo.Domain = DOMAIN;
//proc.StartInfo.UserName = USERNAME;
//proc.StartInfo.Password = BuildPasswordString();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = EXEC_PATH;
proc.StartInfo.Arguments = EXEC_ARGS;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.OutputDataReceived += proc_OutputDataReceived;
proc.BeginOutputReadLine();
proc.WaitForExit();

However, as soon as I uncomment those three lines regarding user credentials, everything stops working. The code executes without an error, but the process doesn't run and no output is received in my callback method.

How can I execute a separate process, collect its output asynchronously as it is generated, while using a specific user's credentials that don't match the executing process?

Update:
Building off of @Dos095-russ's answer, I tested out the same code with a console application instead of ASP.NET. It does work. So it is something within the ASP.NET environment that is causing the failure.

RationalGeek
  • 9,425
  • 11
  • 62
  • 90

2 Answers2

2

I spent almost 2 days to solve this problem... Here is the solution: http://forums.asp.net/p/1032763/3054483.aspx

Briefly, you need to set the "App Pool's Identity" same as your "proc.StartInfo.UserName" and "proc.StartInfo.Password".

You can either change the Identify of "DefaultAppPool" or create a new "App Pool" using your "proc.StartInfo.UserName" as the Identify.

Of course, if you create a new "App Pool", you have to assign your asp.net web application use this "App Pool".

Hope it's useful, good luck. lzch

Larry Lian
  • 21
  • 2
0

I think the problem is in security mechanism of UAC: your initial application have no rights to read output of the another user's process (even if it is a child process). Try to start your application with administrator privilegies and see if you code works.

I think the best way to solve your problem is to redirect your child process output to a file and make a thread in your initial apllication to check that file for updates.

Also found a similar problem Get error and standard output from an elevated child process

Community
  • 1
  • 1
Dmitriy Konovalov
  • 1,777
  • 14
  • 14
  • I'm executing from ASP.NET. Running it under an admin account is undesirable. But I will move the code to a console app to see if the symptom goes away. Interesting idea on redirecting output to a file and then reading the file, but the child process doesn't even seem to *execute*. I have it outputting to a file right now and that file doesn't get updated when it is run with user credentials. – RationalGeek Dec 02 '11 at 13:10
  • Oh, you did not mentioned ASP.NET. As far as i know web application is run under special account defined at installation. That means that your exe is placed somewhere inside the asp folder. Is it possible that user you specified have no access right to that folder? – Dmitriy Konovalov Dec 02 '11 at 14:55
  • I ran it in console app and it worked so ASP.NET is definitely the culprit. I'm going to try running ASP.NET process as system account to see if it works like that. – RationalGeek Dec 02 '11 at 15:04