【问题标题】:Win32Exception when trying to run a .bat file as a specific user尝试以特定用户身份运行 .bat 文件时出现 Win32Exception
【发布时间】:2011-08-20 00:45:03
【问题描述】:

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

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

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

System.ComponentModel.Win32Exception (0x80004005): 访问被拒绝 System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo 开始信息)在 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.");
}

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多