我在一个web.config文件中有两个位置元素,并希望更改后面代码中的授权。
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进行任何更改(任何更改都会有要反映在此文件中,而不是子页面中的其他文件)。填写空白的任何推荐的工作解决方案?目的是允许管理员用户在管理员用户界面上进行更改,以决定允许内部网络上的哪些用户或窗口组查看这两个位置,因此我特别希望从后面的代码中更改允许的角色。 感谢。
答案 0 :(得分:0)
经过多次尝试,放弃尝试对同一文件进行所有更改,而只是在每个location.Path中打开一个新的配置管理器实例。
WebConfigurationManager.OpenWebConfiguration(location.Path),然后将规则应用于每个位置的每个web.config。我无法将所有授权集中在一个地方,但至少它是有效的。