尝试以特定用户身份运行.bat文件时出现Win32Exception

时间:2011-05-06 09:06:16

标签: c#-4.0 permissions asp.net-4.0

我正在尝试从ASP.NET页面在服务器上运行批处理文件。批处理文件需要在某个用户帐户下运行才能成功。

我运行批处理文件的代码没有它在特定帐户下运行正常,(当它在SYSTEM下运行时),但批处理文件无法成功执行。

我需要做的是在服务器上的特定(管理员)帐户下运行它,但是当我添加要在此帐户下运行的代码时,我得到一个例外:

  

System.ComponentModel.Win32Exception   (0x80004005):访问被拒绝   System.Diagnostics.Process.StartWithCreateProcess(的ProcessStartInfo   startInfo)at   System.Diagnostics.Process.Start()

帐户定义存在(它是该机器上唯一的帐户)并具有管理权限。此外,如果我登录到物理机并在帐户下手动运行.bat文件,它可以正常工作。

这是我的代码:

Process m_oProc = new Process();
ProcessStartInfo oInfo = new ProcessStartInfo(batchFileLocation);

oInfo.WorkingDirectory = Path.GetDirectoryName(batchFileLocation);
oInfo.UseShellExecute = false;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;

// ------ This code seems to cause the exception ------ //

string prePassword = "myadminpassword";
SecureString passwordSecure = new SecureString();
char[] passwordChars = prePassword.ToCharArray();
foreach (char c in passwordChars)
{
     passwordSecure.AppendChar(c);
}

oInfo.UserName = "admin";
oInfo.Password = passwordSecure;

// ---------------------------------------------------- //

m_oProc.StartInfo = oInfo;
if (m_oProc.Start())
{
      log.Info("Debug: Process has started successfully");
}
else
{
      log.Error("Some error occured starting the process.");
}

谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

由于这是一个面向内部的应用程序,我只是将应用程序池的标识设置为我想要运行的帐户 - 这解决了我的问题。

相关问题