Solved

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

Posted on 2004-11-01
612 Views
Last Modified: 2012-05-05
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
0
Question by:pavelmed
    14 Comments
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    / 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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     

    Author Comment

    by:pavelmed
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Well then it has to be a permissions issue.
    0
     

    Author Comment

    by:pavelmed
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     

    Author Comment

    by:pavelmed
    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
     
    LVL 17

    Expert Comment

    by:AerosSaga
    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
     

    Author Comment

    by:pavelmed
    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
     
    LVL 17

    Accepted Solution

    by:
    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
     

    Author Comment

    by:pavelmed
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now