How to execute a DOS command from an ASP.NET application deployed on the remote network server?

I have to map a samba share on a remote server from an ASP.NET application using the NET USE command.  I was able to do it if the ASP.NET application was created, compiled, and located on my local server.  But after I move it to the network server, the NET USE returns "Access Denied".  To simplify the analysis, I am trying to execute a simple copy command instead, but still, it works on the local server, but does not work on the remote one.

I probably either have permissions problem or miss something doing the deployment.
I use the following C# code:

try
{
// some code . . . . . . . .

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.UseShellExecute = false;

proc.StartInfo.FileName="c:\\Devel\\uploads\\CopyFile.bat";
proc.Start();
/* the CopyFile.bat contains just one statement:
copy c:\devel\uploads\test1.txt C:\devel\test\test1.txt
*/

//// other code…
// . . . . . . . . . . . . . . . . . . . . . . . .
}
catch (Exception exc)
{
//   The code to display the exc.Message
//. . . . . . . . . . . .
}

Again, when I try this code on a local server, a file is being copied.
After I move the application to the remote server, exception “Access Denied” is being thrown.

I tried to add the ASPNET account on the remote server to the Administrators group.  It did not work.
On the local server it works OK without adding the ASPNET account to the Administrators group.

To deploy, I just copied the application’s files to the appropriate remote server’s IIS directory.
The page is displayed OK, and only the attempt to execute a command fails.

If I execute the CopyFile.bat from the Windows START/Run box, it works OK on both local and network server, therefore the copy command is OK.

Any suggestions will be much appreciated.
Thanks
pavelmedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AerosSagaCommented:
Process MyProcess = new Process();
MyProcess.StartInfo.FileName = "cmd.exe";
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;                
MyProcess.StartInfo.RedirectStandardInput = true;

MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.Start();

StreamWriter MyStreamWriter = MyProcess.StandardInput;
StreamReader MyStreamReader = MyProcess.StandardOutput;

MyStreamWriter.AutoFlush = true;
MyStreamWriter.Write(@"c:\test.bat" + System.Environment.NewLine);
MyStreamWriter.Write("exit" + System.Environment.NewLine);
string s = MyStreamReader.ReadToEnd();
textBox1.Text = s;
0
AerosSagaCommented:
/ Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close();
sOut.Close();

// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

http://dotnetjunkies.com/WebLog/bsblog/archive/2004/05/13/13484.aspx
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

pavelmedAuthor Commented:
I tested the code suggested by AerosSaga.  The result is the same as with my code: The porgram executes OK while running on my local server but throws "Access is denied" exception while starting the process on the network server
(during the execution of the following line:
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
)

So there is some difference between running on a local and remote server, and this difference prevents the program from starting a process on a remote server.


Any suggestions will be much appreciated.
Thanks

0
AerosSagaCommented:
Well then it has to be a permissions issue.
0
pavelmedAuthor Commented:
Yes, the chances are this is a permission issue.  But how to deal with it?  The program starts the process OK on a local server.  What is the difference for the remote server?

Thanks
0
AerosSagaCommented:
well it either has to be the account the asp.net application is running under or the remote machine account.  I'd start with those two.
0
AerosSagaCommented:
I suspect the account your asp.net application is running under does not have sufficient privaleges on the remote machine you are trying to execute the .bat on.
0
pavelmedAuthor Commented:
I assume that the application runs under ASPNET account on both local and remote servers.
On both machines this account is a member of Users group which has the same permissions setting.
The security setting for the IIS directory is also the same on both machines.

What are other privilegis that I could miss?

Thanks
0
AerosSagaCommented:
It is still acting under the first asp.net account when it access the .bat file, so that account will need to be a domain account.  
0
pavelmedAuthor Commented:
I was unable to get the ASP.NET application to run the batch file on the network server while it was working on my local server.
The difference is that the network server is a Windows 2003 Server which has a tighter security.

Does anyone have a suggestion how to run a batch file from an ASP.NET application on a Windows 2003 Server?
Or any other way to accomplish the goal: upload a file to a samba share directory?

Thanks a lot!!
0
AerosSagaCommented:
are you doing these things?

1. I created a domain user to give write permissions to each of the share drives on each server.
2. I used impersonation (http://support.microsoft.com/default.aspx?scid=kb;en-us;306158) to change the user while creating the folder and executing the upload to the user I created.
<identity impersonate = "true"/>
Doing this did 3 things:
1. allowed me to identify the user upon them accessing my page.
2. impersonated a user with write permissions to each server while executing the upload.
3. returned the authentication back to the users domain credentials after the upload.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pavelmedAuthor Commented:
I tried to test the impersonation setting <identity impersonate = "true"/> on my local server (Windows XP) where the application actually works without the impersonation (it executes the batch file).  However, after I added the impersonation setting to the web.config file, it stopped copying the file without any warning or exceptions. (The bach file for the test contained just one statement:  copy c:\devel\uploads\test1.txt C:\devel\uploads\test\test1.txt ).  Correspondingly, it did not work on the Windows 2003 Server (although it also stopped throwing exception "Access Denied").

On my local server I tried to use the impersonation setting with credentals
<identity impersonate = "true" userName="myname" password="mypass" /> where myname and mypass belong to the administation group, however, the file was not copied either.

So the impersonation dod not work for me even on a local server.  I could have done something wrong with the impersonation setting, but I don't see what.

I have also tried to create a domain user.  Not sure I did it correct.  I added my user name with a domain_name\ prefix to the Power Users group using the Computer Management tool.  How else can I create the the Domain user?

Finally, I think I need to run a batch file on the server because I need to copy an uploaded file to the samba share directory.
If I needed just to copy a file, I would just use the PostedFile.SaveAs("... ") - it worked OK for the server's folder.
However, I need to copy a file to a samba share, and for a real application I use the batch file to create a map to that share and then to copy a file to that map.  Again, it does not work on a Windows 2003 server because I can't execute the batch file.
Any other suggestions how to do it???

Thanks a lot
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.