How to execute EXE file on Windows 2008 using ASP.NET - c#

How to execute EXE file on Windows 2008 using ASP.NET and IIS7? I have tried to run code below but it seems do not work. The code is executed without any error but I do not see Notepad in Processes list. Everything works fine if I use ASP.NET developer server but not with IIS7.
string enginePath = "notepad";
var password = new System.Security.SecureString();
foreach (char character in ConfigurationManager.AppSettings["Credentials"])
password.AppendChar(character);
var p = new Process
{
StartInfo =
{
FileName = enginePath,
UseShellExecute = false,
}
};
p.StartInfo.UserName = "Administrator";
p.StartInfo.Password = password;
p.Start();

In response to security concerns Microsoft has continually increased the isolation of the IIS process such that you can not (or I have not found a work around which will allow you to...) invoke anything outside the IIS application domain directly. You can build a Windows service which can use WCF channels to cross that boundary indirectly. That service (configured to be allowed to interact with the desktop) can in turn launch your Notepad.exe.
It is a bit of a kludge to make it happen and I will certainly be interested to see if someone else has a better answer.

I have removed lines below and it suddenly started working. I can't explain how it is possible to run exe under IIS7 account but it is working.
p.StartInfo.UserName = "Administrator";
p.StartInfo.Password = password;

Related

C# process cannot run on IIS

I have a web application that has to open a process and wait for exit.
This process is a .sh script that uses Cygwin.
I'm using the following code:
var process = new Process();
var string processFileLocation = #"C:\script.sh";
var string workingDirectoryLocation = #"C:\script";
var processInformation = new ProcessStartInfo(processFileLocation)
{
WorkingDirectory = workingDirectoryLocation,
UseShellExecute = true
};
process.StartInfo = processInformation;
process.Start();
process.WaitForExit();
If I run the application using IISExpress, everything works fine, the script is being callse.
When I add it to IIS, the process simply gets blocked, I never receive any answer from the request that should call the process.
I added "Full control" permission to that folder for the Application Pool that the website uses, but still no diference.
Any idea why it is behaving like this?
Whenever something stops running when put in IIS, there's a high likelihood that it's due to insufficient permissions. Start by running your pool with an administrator account. If it solves the issue, then you can work your way from there to find what permission you were missing.

Starting a process

As part of SharePoint automation testing, I am trying to open Internet Explorer as another user by using the System.Diagnostics.Process . Here is the following C# code
System.Diagnostics.Process p = new System.Diagnostics.Process();
// Domain and User Name:
p.StartInfo.Domain = "MYDOMAIN";
p.StartInfo.UserName = "myusername";
// Command to execute and arguments:
p.StartInfo.FileName = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
p.StartInfo.Arguments = "http://url/AllItems.aspx";
// Build the SecureString password...
System.String rawPassword = "thepassword";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in rawPassword)
{
encPassword.AppendChar(c);
}
p.StartInfo.Password = encPassword;
// The UseShellExecute flag must be turned off in order to supply a password:
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
When I run this automated test Visual Studio returns informing me that the test was successful, however Internet Explorer does not open.
Is there something in my code I am missing in order for a window to appear? There is no iexplore process running prior to the test being run.
putting double quotes around the file path (since it contains spaces) may help:
p.StartInfo.FileName = "\"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe\""
^^ ^^
In addition, if your intention is to start this from a service process or dll running in a service such as "SharePoint", then this code will probably not launch the process in the desktop desired. You'll need to set the desktop to "winsta0\\default" in the startup info.
To run a process the worker process should have high privileges and this is not an ideal case in any web application. If your purpose is to use IE for unit testing then I would consider using something like WaitIN. If your purpose is for application logic to access a URL and do something then consider using HttpWebRequest. If you still need a process to be started then create a Windows Service and then expose a web call so in Share Point you can just make a call and your Windows Service can run on local account or some other high privilege account.
Hope this helps and please provide the scenario why you want to start the IE and that can give you a better answer in the forum.

Process Start IIS Server

I can not call Process.Start("DocToPDF.exe", "file.docx file.pdf") successfully from IIS.
I have tried allowing desktop interaction from IIS Admin Service;
I have tried granting full control permissions to DefaultAppPool for the directory containing the DocToPDF.exe
None of this worked. The DocToPDF.exe exits with code: 1 (I write the exit code to a file...). When I run the website in debug mode (F5) the program exits with code: 0 and everything is ok. It works perfectly well in debug mode. My guess is it has something to do with IIS permissions or something like that because as I said it works well when launching the application from visual studio.
Here is the method that I am using:
public byte[] DocToPdf(MemoryStream ms)
{
string fileName = Guid.NewGuid().ToString();
string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\" + fileName + ".docx";
string newPath = path.Replace("docx", "pdf");
FileStream fs = new FileStream(path.ToString(), FileMode.OpenOrCreate);
fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
fs.Close();
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = #"C:\Utils\OfficeToPDF.exe";
p.StartInfo.Arguments = path + " " + newPath;
p.Start();
p.WaitForExit();
System.IO.File.WriteAllText(#"C:\Utils\exitcode.txt", p.ExitCode.ToString());
var bytes = System.IO.File.ReadAllBytes(newPath);
System.IO.File.Delete(path);
System.IO.File.Delete(newPath);
return bytes;
}
It is almost always a horrible idea to call any process from an IIS web site/application.
IIS and its worker processes run in session 0. The session isolation can lead to issues if the extra executable requires to be run in a user session. (It is very likely that you hit this, as if the executable attempts to use printing drivers it will hit problems under session 0.)
IIS worker processes are running under application pool identities (without user profile loading if you don't change pool settings). That kind of identities can also lead to failures as the executable might require a normal user account.
There are far too many executable fail to run under IIS, so yours is not something new or uncommon.
Meanwhile, when you hit F5 in Visual Studio, your app is running under ASP.NET Development Server, or IIS Express by default, and that's in your user session. Anything works there can break when switching to IIS. So don't be surprise how little you understand about IIS/Windows. Every experts were beginners.
Typical workaround is to find an alternative way that either supported or recommended by Microsoft or a third party vendor. Note that free and open source things are usually developed or tested without such session 0 case in mind, and they can lead to more issues than you thought.

The value '' is invalid

I have an ASP.NET application to create distribution lists. It works fine on my development machine using IIS Express, but on the server, I am getting the following error from the standard output:
The value '' is invalid.
Below is the code I am using to mail-enable the distribution list. I have replaced the powershell script with a simple "echo test" batch file, but the same error still comes from the standard output.
//ProcessStartInfo info = new ProcessStartInfo("c:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "c:\\PowershellScripts\\EnableDL.ps1 -dlName '" + model.Name + "'");
ProcessStartInfo info = new ProcessStartInfo("c:\\powershellscripts\\test.bat");
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.CreateNoWindow = true;
info.Domain = "CONTOSO.COM";
info.UserName = User.Identity.Name;
info.Password = securePassword;
using (Process run = Process.Start(info))
{
ModelState.AddModelError("dlcreate", run.StandardOutput.ReadToEnd());
run.WaitForExit();
}
I just don't understand what could be causing this. Running the script on my dev machine (IIS Express) works fine, and running the script as the user on the server via CMD also works fine. I can't even reproduce the error via CMD. Any ideas?
UPDATE: Not sure if it is of any help, but when I remove the Domain, Username, and Password objects, all executables run without issue under the DefaultAppPool user.
I think you may need "Full trust" on your ASP.Net app to launch something in the shell. Here is a link talking about trust levels on Web Applications.
http://msdn.microsoft.com/en-us/library/vstudio/330a99hc(v=vs.100).aspx
For those that are getting this problem. I have found the issue to be that the user which you are impersonating does not have permissions to run the application within the desktop of the IIS service.
The sample code linked at the bottom of This Page fixed it for me.

Permission issue running .exe from ASP.NET web application

We have a .exe which we need to execute at the time an order is placed on a website. When
we test this locally it works fine using IIS Express. When we move it to IIS, it fails. We assume this is a permissions error as if we set the App Pool to run as the administrator then the script works again. The question we have is how do we execute the .exe as the administrator whilst the App Pool is ApplicationIdentity? We are using the following code:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = executablePath,
Arguments = argumentList,
Domain = domain,
UserName = userName,
Password = securePassword,
UseShellExecute = false,
LoadUserProfile = true
}
};
process.Start();
process.WaitForExit();
process.Close();
The .exe is trying to write to the Users AppData folder which is why it fails. It is a 3rd party app so we cannot change the source code.
EDIT: Just to clarify also, when we specify the username and password in procmon it still appears to run from ISUR.
We fixed this by enabling User profile on IIS AppPool and setting permission for the IIS user on the folder it was trying to write to.
We sue ProcMon to find where the program was failing and the folder it was trying towrite to was C:\Windows\System32\config\systemprofile
i dont remember actually, but one of them is working 100% ( i had this issue before)
just let me know ehich one of them is the correct one.

Categories