CreateProcessAsUser, web form, Access denied error

R

robgallen

I'm having a wierd issue trying to launch a robocopy process via a web
form. To cut a long story short, it works fine when I run it from the
server it is hosted on, but when I access the site from my own machine,
I get access denied errors.

Example on server 'wwwdeploy':
http://localhost/Deploy/default.aspx effectively runs this command:
robocopy \\wwwdeploy\wwwroot$\project\subfolder
\\wwwtest\drop\project\subfolder /S /Z

Example on my machine:
http://wwwdeploy/Deploy/default.aspx should run the same command:
robocopy \\wwwdeploy\wwwroot$\project\subfolder
\\wwwtest\drop\project\subfolder /S /Z

I can't figure out what sort of security I'm not getting right to allow
files to be copied to the destination (\\wwwtest). I have no access to
change permissions on this server, which is why I'm trying to
authenticate the robocopy process as myself. Any ideas from all you
clever people out there?

Here's the guts of the code I'm using:
public static string ExecuteRobocopy(string vRoot, CopyType copyType,
bool isImageCopy)
{
string destination;
string source;
MungeResourcePath(copyType, out destination, isImageCopy, out source,
vRoot);

_currentSourcePath = source;
Process proc = new Process();
string robocopyOutPut = string.Empty;
try
{
proc = InitialiseProcess(copyType, out robocopyOutPut, source,
destination);
robocopyOutPut = Regex.Replace(robocopyOutPut, "\t", " ");
}
catch (Exception ex)
{
string ipAddress =
HttpContext.Current.Request.ServerVariables["REMOTE_ADDRESS"];
string strError = String.Format("There was an error copying files
from: {0} to {1}. Error: '{2}'",
source, destination, ex.Message);
Logger.LogError(ipAddress, strError);
robocopyOutPut = strError;
}

return robocopyOutPut;
}

private static Process InitialiseProcess(CopyType copyType,
out string robocopyOutPut, string source, string destination)
{
Process proc = new Process();
robocopyOutPut = "You do not have access to robocopy.exe";

// security information, to launch process as a user
string domain = String.Empty;
string userName = String.Empty;
SecureString ssPassword = new SecureString();
string[] user = HttpContext.Current.User.Identity.Name.Split('\\');
domain = user[0];
userName = user[1];
string password = HttpContext.Current.Session["password"] as string;
foreach (char c in password)
{
ssPassword.AppendChar(c);
}

if (domain != String.Empty && userName != String.Empty &&
ssPassword.Length > 0)
{
ProcessStartInfo procInfo = new ProcessStartInfo();

// parameters for process
procInfo.UseShellExecute = false;
procInfo.RedirectStandardInput = true;
procInfo.RedirectStandardOutput = true;
procInfo.RedirectStandardError = true;
procInfo.CreateNoWindow = true;
procInfo.FileName = "robocopy.exe"; // has to be in PATH as no
WorkingDirectory set
// security information
procInfo.Domain = domain;
procInfo.UserName = userName;
procInfo.Password = ssPassword;

//Get the arguments for our process
ProcessArgs procArgs = new ProcessArgs(copyType);

string strArgCopy = procArgs.CopyArgs;
string strArgFiles = procArgs.FilesList;
string strArgRetry = procArgs.RetryOptions;
string strArgLog = procArgs.LoggingOptions;

procInfo.Arguments =
String.Format("{0} {1} {2} {3} {4} {5}",
source, destination, strArgCopy, strArgFiles, strArgRetry,
strArgLog);
proc = Process.Start(procInfo);
proc.WaitForExit();

robocopyOutPut = proc.StandardOutput.ReadToEnd();
proc.Close();
}
return proc;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top