如何添加,并从asp.net web.config中的位置路径中删除授权用户 [英] How can I add and remove authorised users from web.config location path in asp.net

查看:94
本文介绍了如何添加,并从asp.net web.config中的位置路径中删除授权用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图添加和web.config中编程方式删除用户的认可。我使用Windows身份验证。

I am trying to add and remove authorised users from web.config programmatically. I am using Windows Authentication.

这是我对web.config中

This is what I have on web.config

  <location path="Admin">
    <system.web>
      <authorization>
        <allow users="domain\user1, domain\user2"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

现在在code,我有以下code。

Now in the code I have the following code.

protected void UpdateUsers()
{
    System.Configuration.Configuration config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
    ConfigurationLocationCollection section = config.Locations;

    foreach (ConfigurationLocation location in section)
    {
        if(location.Path == "Admin")
        {
            AuthorizationSection admin_section = (AuthorizationSection)config.GetSection("system.web/authorization");

            AuthorizationRule thisAuth = new AuthorizationRule(AuthorizationRuleAction.Allow) ;
                thisAuth.Users.Add("domain\\username");

             admin_section.Rules.Add(thisAuth);
             admin_section.CurrentConfiguration.Save();
        }

    }
}

以上code是增加对System.Web程序,而不是在管理位置的部分。

The above code is adding the section on the system.web and not on the admin location.

推荐答案

我想出答案。这里是更新code。

I figured out the answer. Here is the updated code.

protected void UpdateUsers()
{
    Configuration config = (Configuration)WebConfigurationManager.OpenWebConfiguration("~");
    AuthorizationSection root_section = (AuthorizationSection)config.GetSection("system.web/authorization");

    //Remove all Current Users to root location.
    root_section.Rules.Clear();

    //Add New Users to root location.
    AuthorizationRule rootAuth = new AuthorizationRule(AuthorizationRuleAction.Allow);
    rootAuth.Users.Add("domain\\rootusername1");
    rootAuth.Users.Add("domain\\rootusername2");
    rootAuth.Users.Add("domain\\rootusername3"); 
    root_section.Rules.Add(rootAuth);

    ////Add Deny All Users to root location.
    AuthorizationRule rootDeny = new AuthorizationRule(AuthorizationRuleAction.Deny);
    rootDeny.Users.Add("*");
    root_section.Rules.Add(rootDeny);

    root_section.CurrentConfiguration.Save();

    //Other Locations  
    ConfigurationLocationCollection section = config.Locations;

    foreach (ConfigurationLocation location in section)
    {
        if (location.Path == "admin") //This is case Sensitive
        {
            Configuration adminConfig = (Configuration)location.OpenConfiguration();
            AuthorizationSection admin_section = (AuthorizationSection)adminConfig.GetSection("system.web/authorization");

            //Remove all Current Users to admin location.
            admin_section.Rules.Clear();

            ////Add New Users to admin location.
            AuthorizationRule adminAuth = new AuthorizationRule(AuthorizationRuleAction.Allow);
            adminAuth.Users.Add("domain\\adminusername1");
            adminAuth.Users.Add("domain\\adminusername2");
            adminAuth.Users.Add("domain\\adminusername3");
            adminAuth.Users.Add("domain\\adminusername4");
            admin_section.Rules.Add(adminAuth);
            adminAuth = null;

            ////Add Deny All Users to root location.
            AuthorizationRule adminDeny = new AuthorizationRule(AuthorizationRuleAction.Deny);
            adminDeny.Users.Add("?"); // For some reason if I remove this line it says "Object reference not set to an instance of an object"
            adminDeny.Users.Add("*");
            admin_section.Rules.Add(adminDeny);

            admin_section.CurrentConfiguration.Save();
        }

    }
}

希望这将是有益的人。

Hope this will be helpfull for someone.

这篇关于如何添加,并从asp.net web.config中的位置路径中删除授权用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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