【问题标题】:Running a Batch (which run an exe) from ASP .NET从 ASP .NET 运行批处理(运行 exe)
【发布时间】:2014-07-03 18:54:20
【问题描述】:

我在一个调用外部 bat 的 ASP .NET 2.0 webapp 中有以下代码:

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;

但这个过程会永远挂起。

【问题讨论】:

  • 您是否在 Active Directory 下使用用户?
  • @Daniele 不,都在本地服务器中

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


【解决方案1】:

你可以试试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.
    }
}

【讨论】:

    猜你喜欢
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 2011-10-29
    相关资源
    最近更新 更多