欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-10-21 11:33:54
using system;using system.data;using system.configuration;using system.collections;usi...

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);
    }
}