在我的代码隐藏类中,如何检索授权角色?

Ric*_*ema 6 asp.net web-config roles

我在web.config中有以下内容:

<location path="RestrictedPage.aspx">
    <system.web>
        <authorization>
            <allow roles="Group1Admin, Group3Admin, Group7Admin"/>
            <deny users="*"/>
        </authorization>
    </system.web>
</location>
Run Code Online (Sandbox Code Playgroud)

在RestrictedPage.aspx.cs中,如何检索包含Group1Admin,Group3Admin和Group7Admin的允许角色集合?

这就是为什么我问:

web.config正在处理页面的授权.这很好.但是我将会有几个这样的页面(比如RestrictedPage.aspx,RestrictedPage2.aspx,RestrictedPage3.aspx).这些页面中的每一个都将在其上进行自定义webcontrol.每个页面都有不同的允许角色.我的webcontrol有一个下拉列表.下拉列表中的选项取决于用户角色与页面允许角色的交集.

如下所述,使用XPath搜索web.config可能会有效.我只是希望有更多的框架.有点像SiteMap.当我在我的web.sitemap中放置角色时,我可以使用SiteMap.CurrentNode.Roles抓取它们(我的网站使用的是Windows身份验证,所以我不能使用web.sitemap进行安全修整,而是宁愿只保留一个角色文件).

hma*_*mak 3

// 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");
Run Code Online (Sandbox Code Playgroud)

从部分对象获取 AuthorizationRuleCollection 对象,然后您可以在其中提取角色。

注意:您可能需要稍微修改该部分的路径,因为您从“location path="RestrictedPage.aspx"”开始,我没有尝试这种情况。