我的 web.config 中有以下内容:
<location path="RestrictedPage.aspx">
<system.web>
<authorization>
<allow roles="Group1Admin, Group3Admin, Group7Admin"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
在 RestrictedPage.aspx.cs 中,如何检索包含 Group1Admin、Group3Admin 和 Group7Admin 的允许角色集合?
这就是我问的原因:
web.config 正在处理对页面的授权。这很好用。但我将有几个这样的页面(比如 RestrictedPage.aspx、RestrictedPage2.aspx、RestrictedPage3.aspx)。这些页面中的每一个都将有我的自定义 web 控件。这些页面中的每一个都将具有不同的允许角色。我的 webcontrol 有一个下拉列表。下拉列表中的选择取决于用户角色和页面允许角色的交集。
如下所述,使用 XPath 搜索 web.config 可能会起作用。我只是希望有更多的框架-y。有点像站点地图。当我将角色放入我的 web.sitemap 时,我可以使用 SiteMap.CurrentNode.Roles 来获取它们(我的网站使用 Windows 身份验证,所以我不能使用 web.sitemap 进行安全调整,我宁愿只保留一个角色文件)。
最佳答案
// set the configuration path to your config file
string configPath = "??";
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);
// Get the object related to the <identity> section.
AuthorizationSection section = (AuthorizationSection)config.GetSection("system.web/authorization");
从 section 对象获取 AuthorizationRuleCollection 对象,然后您可以在其中提取角色。
注意:您可能需要稍微修改该部分的路径,因为您从“location path="RestrictedPage.aspx""开始,我没有尝试这种情况。
关于asp.net - 在我的代码隐藏类中,如何检索授权角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/213784/