【发布时间】: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