【问题标题】:How to set the directory permission in .NET 3.5?如何在 .NET 3.5 中设置目录权限?
【发布时间】:2014-01-31 07:09:57
【问题描述】:

我是 .NET 的新手。当我上传图片时,我收到一个错误

System.UnauthorizedAccessException:
拒绝访问路径“C:\Inetpub\vhosts\cmcnoida.com\httpdocs\i_image\123”。

此代码在本地运行良好,但在实时服务器上生成上述错误。我能做什么?

我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    string t_sname, t_cname, t_pack, t_college, t_djoin;

    if (TextBox2.Text == "")
    { t_sname = "-"; }
    else
    { t_sname = TextBox2.Text; }
    if (TextBox3.Text == "")
    { t_cname = "-"; }
    else
    { t_cname = TextBox3.Text; }
    if (TextBox4.Text == "")
    { t_pack = "-"; }
    else
    { t_pack = TextBox4.Text + " lacs pa"; }
    if (TextBox5.Text == "")
    { t_college = "-"; }
    else
    { t_college = TextBox5.Text; }
    if (TextBox6.Text == "")
    { t_djoin = "-"; }
    else
    { t_djoin = TextBox6.Text; }

    // conn = new SqlConnection("Data Source=USER-PC;Initial Catalog=cmcnoida;Integrated Security=True");
    conn = new SqlConnection("Data Source=127.0.0.1;Integrated Security=False;User ID=kvch_db;Connect Timeout=200;Encrypt=False;Packet Size=4096;Database=cmcnoida;password=kv_12_2014");
    //conn = new SqlConnection("server=singhal;database=abc;Trusted_Connection=yes");
    comm = new SqlCommand();
    comm.Connection = conn;
    comm.CommandText = "select max(id) from placement";
    conn.Open();
    int i = (int)comm.ExecuteScalar();
    conn.Close();
    string a = (i + 1).ToString();

    DirectoryInfo dd2 = new DirectoryInfo(Server.MapPath("~\\i_image\\" + a));

    dd2.Create();
    dd2.Refresh();

    string fup;
    if (FileUpload1.HasFile == true)
    {
        fup = "~\\i_image\\" + a + "\\" + FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath(fup));
    }
    else
    {
        fup = "~\\i_image\\" + a + "\\dummy-man.jpg";
        File.Copy(Server.MapPath("~\\admin\\dummy-man.jpg"), Server.MapPath("~\\i_image\\" + a + "\\dummy-man.jpg"));
    }
    comm.Connection = conn;
    comm.CommandText = "insert into placement values('" + t_sname + "','" + t_cname + "','" + t_pack + "','" + t_college + "','" + t_djoin + "','" + fup + "')";
    conn.Open();
    comm.ExecuteNonQuery();
    conn.Close();
    // binddatagrid();
    TextBox2.Text = "";
    TextBox3.Text = "";
    TextBox4.Text = "";
    TextBox5.Text = "";
    TextBox6.Text = "";
    TextBox2.Focus();
    Response.Write("<script language=JavaScript> alert('Placement Record Inserted !!'); </script>");
}

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 设置应用程序外部的权限。除非使用模拟,否则用于 ASP.NET 应用程序池的帐户可能需要被授予写入权限。

标签: asp.net .net iis asp.net-3.5


【解决方案1】:

如果您将其托管在您的服务器上,您需要设置文件夹的权限。这是What are all the user accounts for IIS/ASP.NET and how do they differ?的好读物

但我怀疑这里不是这样。我认为您将其托管在此处的共享环境中。
如果是这种情况,您无法使用代码设置服务器文件夹安全权限。因此,您必须联系您的托管服务提供商并要求他们授予 i_image 文件夹的权限。

我提到 i_image 文件夹是因为子文件夹 123 在您的代码中看起来是动态的。给根文件夹设置权限就够了。

【讨论】:

    【解决方案2】:

    在许多涉及访问安全性的情况下,您需要使用 ASP.NET 模拟(作为 Windows 用户帐户运行)。然后您需要在 web.config 中进行一些更改。

    <configuration>
      <system.web>
        <identity impersonate="true"/>
      </system.web>
    </configuration>
    
    <identity impersonate="true" userName="DOMAIN\UserName" password="***" />
    

    您需要提供对您要写入的文件夹具有写入权限的用户凭据。

    这些设置也可以在 IIS 中的应用程序池中完成。

    【讨论】:

      猜你喜欢
      • 2019-05-11
      • 2010-09-12
      • 2014-05-28
      • 2019-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      相关资源
      最近更新 更多