如何处理System.Security.SecurityException [英] How to deal with System.Security.SecurityException

查看:155
本文介绍了如何处理System.Security.SecurityException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,



我正在尝试在asp.net中运行批处理文件,我的代码在离线模式下正常工作,



但是当我正在制作时,它给我System.Security.SecurityException错误,

并且无法运行代码,即使我已经web.config文件中的更改。



请帮助我。



Hello Experts,

I am trying to run a batch file in asp.net, where my code is properly working on offline mode,

but when i am making live, its giving me "System.Security.SecurityException" error,
and not able to run the code, even i have been changes in web.config file.

kindly help me.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        if(Page.IsPostBack)
        {
            
    string exec = TextBox1.Text;
    // Get the full file path
    string strFilePath = Server.MapPath("fine.bat");

    // Create the ProcessInfo object
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardError = true;


    // Start the process
    System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

    // Open the batch file for reading

    //System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
    System.IO.StreamReader strm = proc.StandardError;
    // Attach the output for reading
    System.IO.StreamReader sOut = proc.StandardOutput;

    // Attach the in for writing
    System.IO.StreamWriter sIn = proc.StandardInput;

    sIn.WriteLine(exec);

    strm.Close();


    // Exit CMD.EXE
    string stEchoFmt = "# {0} run successfully. Exiting";

    sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
    sIn.WriteLine("EXIT");

    // Close the process
    proc.Close();

    // Read the sOut to a string.
    string results = sOut.ReadToEnd().Trim();

    // Close the io Streams;
    sIn.Close();
    sOut.Close();

    // Write out the results.
    string fmtStdOut = "<font face=courier size=0>{0}</font>";
    this.Response.Write("<br>");
    this.Response.Write("<br>");
    this.Response.Write("<br>");
    this.Response.Write(String.Format(fmtStdOut, results.Replace(System.Environment.NewLine, "<br>")));

}
}


}

推荐答案

可能是权限问题。

实时机器比开发机器更安全地锁定,所以你需要查看每个机器的访问权限(特别是写权限)您正在引用的文件和文件夹。 (并且在网站中使用相对路径和固定文件名都是非常非常糟糕的想法。相对路径是相对于当前页面的,所以如果你的网站结构正确,你将像五彩纸屑一样散布文件 - 它是一个更好的想法是拥有一个文件或资源文件夹,并通过MapPath引用它。而固定文件名在Web应用程序中是一个非常糟糕的主意,因为它们按定义是多用户。使用临时文件名或Guids,和自己清理!)
Probably, it's a permissions issue.
Live machines tend to be more securely "locked down" than your development machine, so you need to look at the access permissions (and particularly the write permissions) of every file and folder you are referencing. (And using relative paths and fixed file names in a web site are both very, very bad ideas anyway. Relative paths are relative to the current page, so if your site is structured properly, you will be scattering files around like confetti - it's a better idea to have a single "Files" or "Resources" folder and reference that via MapPath instead. And fixed file name are a very bad idea in web app since they are by definition multi user. Use temporary file names, or Guids, and clean up after yourself!)


这篇关于如何处理System.Security.SecurityException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆