20

My current situation is that I need to execute an exe(which creates a local .txt file) in remote server with IIS hosting an ASP.net/C# API. I created a local user(say userA) as admin to run the web service in the remote server but the .txt file was not created. I already checked and granted necessary folder permissions to userA and added the user in various groups. The funny thing is that if i am logged in as userA in the remote system, the exe gets executed as expected. If i log out then it fails. Server is Win server 2008 with IIS 7. Any help would be appreciated thanks.

UPDATE: I've solved the issue and posted the answer and a few links to related issues here on SO. In short, I needed to set 'load user profile' true in IIS app pool.

Thanks everyone for their contribution

EDIT: Code extracted from comments

Process proc = new Process(); 
proc.StartInfo.FileName = path; 
proc.StartInfo.Arguments = exeparams; 
proc.Start(); 
proc.WaitForExit(); 
stat = proc.ExitCode; 
if (stat != 0) 
{ 
    throw new Functions.log("Error"); 
} 
Click Ok
  • 8,700
  • 18
  • 70
  • 106
santhosh.v
  • 651
  • 1
  • 5
  • 10
  • FYI: its NOT an issue of getting code to call the process. It is an issue that the *.exe gets executed ONLY when the IIS user is manually logged into and remains logged the remote system when the API is called – santhosh.v Dec 01 '12 at 10:49
  • Can you please paste the code that you are currently using? I'm interested to see how you are trying to write the response... – beyond-code Dec 01 '12 at 10:50
  • This sounds like an IIS authentication issue. Can you check in IIS and list all the settings under authentication? I'm guessing you have Windows authentication and ASP.NET impersonation on, when you really don't want that. Also go to the advanced settings for your application pool and list the process model identity. That should give us a bit more to work with. – Stephen Oberauer Dec 01 '12 at 11:12
  • Code is; Process proc = new Process(); proc.StartInfo.FileName = path; proc.StartInfo.Arguments = exeparams; proc.Start(); proc.WaitForExit(); stat = proc.ExitCode; if (stat != 0) { throw new Functions.log("Error"); } Like I said. code works fine under the conditions described in question – santhosh.v Dec 01 '12 at 11:23
  • Ok here are the IIS settings. Windows Authentication: disabled; Forms Authentication: disabled; Anon auth: enabled; .Net Impersonation: disabled. In process model ID, managed pipeline: Integrated, identity: admin (currently for testing, previously was userA), applications: 2 – santhosh.v Dec 01 '12 at 11:43

2 Answers2

14

UPDATE: I managed to solve the issue after many weeks. Thank you all for your contribution. Apparently IIS does not load windows user profiles by default. So when running as a different user who is not logged on, their windows profile must be loaded by IIS. In advanced setting menu of your app pool, there is an option "load windows profile" I just changed this to true. In prior versions of IIS, this was set to 'true' by default.

Related questions on SO with same solution:

1) Security exceptions in ASP.NET and Load User Profile option in IIS 7.5

2) Running a asp.net web application project on IIS7 throws exception

3) System.Web.AspNetHostingPermission Exception on New Deployment

Another 4) http://geekswithblogs.net/ProjectLawson/archive/2009/05/05/iis-system.web.aspnethostingpermission-exception-on-windows-7-rc.aspx

Community
  • 1
  • 1
santhosh.v
  • 651
  • 1
  • 5
  • 10
8

You can use Process.Start

Process process = new Process();
process.StartInfo.FileName = "CVS.exe";
process.StartInfo.Arguments = "if any";
process.Start();

There is also a post about running processes as another user in asp.net:

http://weblogs.asp.net/hernandl/archive/2005/12/02/startprocessasuser.aspx

Supplying user credential

In short it says that you must redirect the process, with code like this:

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");

info.UseShellExecute = false;

info.RedirectStandardInput = true;

info.RedirectStandardError = true;

info.RedirectStandardOutput = true;

info.UserName = dialog.User; // see the link mentioned at the top

info.Password = dialog.Password;

using (Process install = Process.Start(info))

{

      string output = install.StandardOutput.ReadToEnd();

      install.WaitForExit();

      // Do something with you output data

      Console.WriteLine(output);

}
  • Thank you.Yes i am infact using Process.start. Its not the issue of calling the process but getting an answer/clarity to my situation. – santhosh.v Dec 01 '12 at 10:45
  • Yes thanks. but I tried that with the local Admin account did not work. :( and I think that brings up a window prompt to input user/pass. The exe is in the remote server and it has to be called without any user interaction. – santhosh.v Dec 01 '12 at 10:50
  • make a try/catch statement and print the error message, what do you get? –  Dec 01 '12 at 11:13
  • Strange enough no error, the exitcode of the process is 'sucess'. but no output file is generated. – santhosh.v Dec 01 '12 at 11:26
  • check the *working directory* of the application. It can be set by ProcessInfo. Make sure it's the same as the executable location. >> ProcessStartInfo.WorkingDirectory = "C:\exe\location\folder" –  Dec 01 '12 at 19:23
  • Already tried that. Same code works when app pool id is set to local admin. – santhosh.v Dec 03 '12 at 05:08
  • Unfortunately only setting app identity to something else than default applicationpoolid works for me. – Hylle Mar 26 '18 at 12:19