简体   繁体   English

从后面的代码在一个web.config中设置多个位置元素的授权部分

[英]Setting authorization section of multiple location elements in one web.config from code behind

I have two location elements in a single web.config file, and would like to change the authorization in the code behind. 我在单个web.config文件中有两个location元素,并想在后面的代码中更改授权。

The web.config location sections are like this: web.config位置部分如下所示:

<configuration>
  <location path="~">
    <system.web>
      <compilation debug="false" targetFramework="4.0"/>
      <authorization>
        <allow roles=""/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

  <location path="~/SubPage">
    <system.web>
      <compilation debug="false" targetFramework="4.0"/>
      <authorization>
        <allow roles=""/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
</configuration>

From the code behind I would like to go through the location elements, and then make the changes to that specific location element. 从后面的代码中,我想遍历location元素,然后对特定的location元素进行更改。 For example, c# code behind would be: 例如,后面的C#代码为:

Configuration myConfig = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationLocationCollection locations = myConfig.Locations;
foreach (ConfigurationLocation location in locations)
{   
   if (location.Path = "~")
   {
     //define & clear the authorization section of the particular location element.
     //create and apply rule for allow in that authorization section.
     //create and apply rule for deny  in that authorization section.
     //save the change

   }

   if (location.Path = "~/SubPage")
   {
     //define & clear the authorization section of the particular location element.
     //create and apply rule for allow in that authorization section.
     //create and apply rule for deny  in that authorization section.
     //save the change

   }

}

I have tried several different things here, but I don't have one solution that actually works so far... I need to consoldiate any change to the web.config in the root directory (~) (any change would have to be reflected in this file, not other files in the subpage). 我在这里尝试了几种不同的方法,但是到目前为止,我还没有一种实际可行的解决方案...我需要将根目录(〜)中对web.config的任何更改合并(必须反映出所有更改) (而不是子页面中的其他文件)。 Any recommended working solution to fill in the blanks? 有什么推荐的可行解决方案来填补空白? The purpose is to allow admin users to make changes on an admin user interface to decide which users or windows groups on the internal network will be allowed to view the two locations, so I would particularly like to change the allowed roles from the code behind. 目的是允许管理员用户在管理员用户界面上进行更改,以决定内部网络上的哪些用户或Windows组将被允许查看这两个位置,因此我特别希望从后面的代码中更改允许的角色。 Thanks. 谢谢。

After several attempts, abandoned trying to make all the changes on the same file, and instead simply opened a new configuration manager instance in each location.Path. 经过几次尝试后,放弃了对同一文件进行所有更改的尝试,而只是在每个location.Path中打开了一个新的配置管理器实例。

WebConfigurationManager.OpenWebConfiguration(location.Path), then applied the rules to each individual web.config at each location. WebConfigurationManager.OpenWebConfiguration(location.Path),然后将规则应用于每个位置的每个单独的web.config。 I couldn't centralize all the authorizations in one place, but at least it was working. 我无法将所有授权集中在一个地方,但至少可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM