Asp.net CMD安全问题

发布于 2024-12-05 10:37:47 字数 2284 浏览 1 评论 0原文

我在 Windows 2008 服务器上遇到权限问题。我有一个命令行实用程序,需要运行它来将一些标准文件转换为 CSV 文件。它们又用于将数据导入到我的 SQL 数据库中。我能够在我的 2003 服务器上正常工作,但我的 Windows 2008 服务器不允许代码运行。下面是代码的简化版本。基本上在这个例子中我只是尝试运行一个简单的命令。但我一直收到输出访问被拒绝的消息。我该如何纠正这个问题?

public partial class _Bank : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void btn_Click(object sender, EventArgs e)
{

    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;
    processStartInfo.RedirectStandardError = true;  

    Process process = Process.Start(processStartInfo);

    if (process != null)
    {

       process.StandardInput.WriteLine("dir");

       process.StandardInput.Close();
       string outputString = process.StandardOutput.ReadToEnd();
       Response.Write(outputString);

       string error = process.StandardError.ReadToEnd();
       Response.Write(error);

    }

}
private string ProcessRunner()
{
    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;

    Process process = Process.Start(processStartInfo);

    if (process != null)
    {

        //process.StandardInput.WriteLine("This is a test line");
        process.StandardInput.WriteLine("c:\\");
        process.StandardInput.WriteLine("This is a test line");

        process.StandardInput.Close(); // line added to stop process from hanging on     ReadToEnd()

        string outputString = process.StandardOutput.ReadToEnd();
        Response.Write(outputString);
        return outputString;

    }

    return string.Empty;
}

public static int ExecuteCommand(string Command, int Timeout)
{
    int ExitCode;
    ProcessStartInfo ProcessInfo;
    Process Process;

    ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
    ProcessInfo.CreateNoWindow = true;
    ProcessInfo.UseShellExecute = false;
    Process = Process.Start(ProcessInfo);
    Process.WaitForExit(Timeout);
    ExitCode = Process.ExitCode;
    Process.Close();

    return ExitCode;
}
}

I am having trouble with permissions on Windows 2008 server. i have a command line utility that I need to run to convert some standard files to CSV files. That are in turn used to import data to my SQL database. I am able to get the done to work fine on my 2003 server but my windows 2008 server is not allowing the code to run. Below is a watered down version of the code. Basically in this example I am just trying to run a simple command. But I keep getting the output access denied. How do I correct this?

public partial class _Bank : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void btn_Click(object sender, EventArgs e)
{

    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;
    processStartInfo.RedirectStandardError = true;  

    Process process = Process.Start(processStartInfo);

    if (process != null)
    {

       process.StandardInput.WriteLine("dir");

       process.StandardInput.Close();
       string outputString = process.StandardOutput.ReadToEnd();
       Response.Write(outputString);

       string error = process.StandardError.ReadToEnd();
       Response.Write(error);

    }

}
private string ProcessRunner()
{
    ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe");
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;

    Process process = Process.Start(processStartInfo);

    if (process != null)
    {

        //process.StandardInput.WriteLine("This is a test line");
        process.StandardInput.WriteLine("c:\\");
        process.StandardInput.WriteLine("This is a test line");

        process.StandardInput.Close(); // line added to stop process from hanging on     ReadToEnd()

        string outputString = process.StandardOutput.ReadToEnd();
        Response.Write(outputString);
        return outputString;

    }

    return string.Empty;
}

public static int ExecuteCommand(string Command, int Timeout)
{
    int ExitCode;
    ProcessStartInfo ProcessInfo;
    Process Process;

    ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
    ProcessInfo.CreateNoWindow = true;
    ProcessInfo.UseShellExecute = false;
    Process = Process.Start(ProcessInfo);
    Process.WaitForExit(Timeout);
    ExitCode = Process.ExitCode;
    Process.Close();

    return ExitCode;
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-12-12 10:37:47

它在带有应用程序池的 asp.net 应用程序中运行。应用程序池有一个标识(本地系统、网络服务等),此过程将被执行。
确保该用户对您计划访问的文件夹具有必要的权限。

恕我直言:出于安全原因,在 Web 应用程序中启动另一个进程不是最佳实践,并且永远不应在 Internet Web 应用程序上执行。

编辑:
通常,用户名为 ASPNET 或 IWAM_[服务器名称](IWAM_... Internet 信息服务的内置帐户,用于启动进程外应用程序)。只需向该用户授予对该文件夹的访问权限即可。

This is running within an asp.net application with an App-Pool. The App-Pool has an identity (LocalSystem, Network Service etc) this process is executed.
Make sure that this user does have the necessary privileges on the folders you are planning to access.

IMHO: for security reasons starting another process within a web application is not best practice and should never be done on internet web applications.

EDIT:
Usualy the user is named ASPNET or IWAM_[Servername] (IWAM_... Built-in account for Internet Information Services to start out of process applications). Just give access to the folder to that user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文