【问题标题】:Setting authorization section of multiple location elements in one web.config from code behind从后面的代码中设置一个 web.config 中多个位置元素的授权部分
【发布时间】:2012-11-20 21:30:30
【问题描述】:

我在一个 web.config 文件中有两个 location 元素,并且想在后面的代码中更改授权。

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>

我想从后面的代码中检查位置元素,然后对特定的位置元素进行更改。例如,后面的 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

   }

}

我在这里尝试了几种不同的方法,但到目前为止我还没有一个真正有效的解决方案...我需要将任何更改合并到根目录 (~) 中的 web.config (任何更改都会反映在这个文件中,而不是子页面中的其他文件)。有什么推荐的工作解决方案来填补空白吗?目的是允许管理员用户在管理员用户界面上进行更改,以决定允许内部网络上的哪些用户或 Windows 组查看这两个位置,所以我特别想从后面的代码中更改允许的角色。 谢谢。

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

    多次尝试后,放弃尝试对同一个文件进行所有更改,而是简单地在每个 location.Path 中打开一个新的配置管理器实例。

    WebConfigurationManager.OpenWebConfiguration(location.Path),然后将规则应用于每个位置的每个单独的 web.config。我无法将所有授权集中在一个地方,但至少它是有效的。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2011-05-21
    • 2020-12-20
    • 1970-01-01
    • 2011-07-30
    相关资源
    最近更新 更多