从ASP .NET运行批处理(运行exe)

时间:2014-05-15 11:00:20

标签: c# asp.net batch-file asp.net-2.0 impersonation

我在ASP .NET 2.0 webapp中有以下代码,它调用外部bat:

using (Process process = new Process()) 
{
    process.StartInfo.FileName = command;
    process.StartInfo.Arguments = args;
    process.Start();

    process.WaitForExit();
    int exitCode = process.ExitCode;
}

此代码工作正常,批处理脚本已成功执行。

如果我尝试从批处理脚本中调用外部可执行文件(无论exe是什么),我收到了与用户权限相关的错误(遗憾的是我没有完整的错误消息,但它看起来像是ASP .NET用户无法运行可执行文件。

我尝试指定此参数以使用指定的用户:

process.StartInfo.UserName = "User";
process.StartInfo.Password = this.ConvertToSecureString("Password");
process.StartInfo.UseShellExecute = false;

但这个过程永远都会挂起。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Microsoft的this旧文章,它描述了一种使用Windows API的方式,出于模拟的目的,在ASP.NET中,不要害怕您将看到的意大利面条代码,只是在:

中实现您的通话
public void Page_Load(Object s, EventArgs e)
{
    if(impersonateValidUser("username", "domain", "password"))
    {
        //Insert your code that runs under the security context of a specific user here.
        undoImpersonation();
    }
    else
    {
        //Your impersonation failed. Therefore, include a fail-safe mechanism here.
    }
}

希望它有所帮助。

相关问题