Asp.net给站点某目录增加Aspnet用户

时间:2021-05-28

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;

using System.IO;
using System.Net;
using System.Security.AccessControl;
using System.Security.Principal;

public partial class ACLChange : System.Web.UI.Page
{
//文件夹路径
private string strDir = "images";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddDirectorySecurity();
}
}

/// <summary>
/// 给文件夹添加ASPNET用户
/// </summary>
private void AddDirectorySecurity()
{
DirectoryInfo dirinfo = new DirectoryInfo(Server.MapPath(strDir));

if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
{
dirinfo.Attributes = FileAttributes.Normal;
}

//取得访问控制列表
DirectorySecurity dirsecurity = dirinfo.GetAccessControl();

string strDomain = Dns.GetHostName();

dirsecurity.AddAccessRule(new FileSystemAccessRule(strDomain + "\\ASPNET", FileSystemRights.FullControl, AccessControlType.Allow));

dirinfo.SetAccessControl(dirsecurity);
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章