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

标签 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>

在 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/

相关文章:

asp.net - 如何修复 Asp.Net 中的 Core Web Vitals FID 问题?

c# - .Net Core/ASP5 应用程序的构建路径是什么?

asp.net - 即使在 applicationHost.config 中允许,gzip 压缩也不起作用

asp.net - Web.config - ConnectionString 属性尚未初始化

model - 如何使用 Grakn/Graql 建模角色层次结构(以及它们之间的关系)?

ubuntu - Ansible:如何使用不同密码的主机运行游戏?

Silverlight - 根据用户角色控制可见性

asp.net - ASP.NET 中是否有一些初始化区域只运行一次?

c# - 还有什么可以导致 WebResource.axd 出现 'Padding is invalid and cannot be removed"异常?

asp.net - 如何使用配置转换删除 web.config 中的模块