.NET 4: Process.Start using credentials returns empty output

I am running an external program from ASP.NET :

var process = new Process(); var startInfo = process.StartInfo; startInfo.FileName = filePath; startInfo.Arguments = arguments; startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; //startInfo.RedirectStandardError = true; process.Start(); process.WaitForExit(); Console.Write("Output: {0}", process.StandardOutput.ReadToEnd()); //Console.Write("Error Output: {0}", process.StandardError.ReadToEnd()); 

Everything works fine with this code: the external program is executed and processed .StandardOutput.ReadToEnd () returns the correct output.

But after I add these two lines before process.Start () (to run the program in the context of a different user account):

 startInfo.UserName = userName; startInfo.Password = securePassword; 

The program is not executed and is being processed. StandardOutput.ReadToEnd () returns an empty string. Exceptions are not thrown.

userName and securePassword are correct (an exception is thrown if the credentials are incorrect).

How to run the program in the context of another user account?

Environment: .NET 4, Windows Server 2008 32bit

UPD:

The application works fine on the ASP.NET + Windows 7 development server, but does not work on IIS 7 + Windows Server 2008 Web Edition.

UPD2:

This is detected in the event log:

Incorrect cryptcp.exe application, version 3.33.0.0, time stamp 0x4be18460, kernel32.dll error module, version 6.0.6002.18005, time stamp 0x49e03821, exception code 0xc0000142, error offset 0x00009eed, process identifier 0xbf4, application start time 0x01caf1b91f5b851a.

cryptcp.exe is the name of the external application.

+6
process.start
source share
4 answers

I understand that this was asked some time ago, but I ran into the same problem and found a solution on this website: Dangers and problems of starting the process under the new Permissions

Application of the solution in the Application section could not be initialized. Corrected it correctly.

Hope this saves you time and frustration!

+3
source share

According to Microsoft, you cannot read standard output and standard error, as it completes the deadlock. To solve the problem, use something like the following:

 private readonly StringBuilder outputText = new StringBuilder(); private readonly StringBuilder errorText = new StringBuilder(); 

. ,.

  process.OutputDataReceived += delegate( object sendingProcess, DataReceivedEventArgs outLine) { if (!string.IsNullOrEmpty(outLine.Data)) { outputText.AppendLine(outLine.Data); } }; process.ErrorDataReceived += delegate( object sendingProcess, DataReceivedEventArgs errorLine) { if (!string.IsNullOrEmpty(errorLine.Data)) { errorText.AppendLine(errorLine.Data); } }; process.BeginOutputReadLine(); process.BeginErrorReadLine(); process.WaitForExit(); Console.WriteLine(errorText.ToString()); Console.WriteLine(outputText.ToString()); 
+2
source share

Looking at the MSDN documentation , there are several other elements that are recommended to be configured to run the application correctly as another user.

  • Set the properties of the domain, username and password (you must set the domain)
  • Set the default working directory with username / password, this is the system32 folder

This can help you solve this problem.

+1
source share

Perhaps the application you are launching needs to load the profile (by default it will not). Have you tried setting the LoadUserProfile property to true?

+1
source share

All Articles