需要 Web.config 授权中的多个角色

发布于 2024-08-30 14:12:11 字数 530 浏览 6 评论 0原文

是否可以在 web.config 文件的授权元素中指定需要多个角色?目前,我在网站的一个 web.config 中针对特定目录设置了此块:

<authorization>  
    <allow roles="Global, Region" />
    <deny users="*" />
</authorization>

我刚刚确定了一种特殊情况,其中具有比全局和区域两个更低级别权限的人也应该有权访问此目录。粗略地说,我想要这样的东西:

<authorization>  
    <allow roles="GlobalManager, RegionManager, SiteManager && FooSite" />
    <deny users="*" />
</authorization>

有什么想法吗?我意识到我可能应该为这种情况安排一个新角色,但我想避免这种情况。谢谢!

Is it possible to specify that multiple roles are required inside the authorization element of the web.config file? I currently have this block in one web.config of my site for a specific directory:

<authorization>  
    <allow roles="Global, Region" />
    <deny users="*" />
</authorization>

I've just identified a special case where a person with two lower-level permissions than Global and Region should also have access to this directory. Roughly, I want something like this:

<authorization>  
    <allow roles="GlobalManager, RegionManager, SiteManager && FooSite" />
    <deny users="*" />
</authorization>

Any ideas? I realize I probably should have a new role for this scenario, but I'd like to avoid that. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑红尘 2024-09-06 14:12:11

我认为您无法通过 web.config 中允许的当前配置来做到这一点。不过,您可以执行以下操作...作为相关页面的 Page_Load 事件中的第一行,请使用以下代码 (VB):

If Not (User.IsInRole("Role1") AndAlso User.IsInRole("Role2")) Then _
    FormsAuthentication.RedirectToLoginPage()

这行当然假设您正在使用 FormsAuthentication。如果没有,您需要根据您的身份验证方法将 FormsAuthentication.RedirectToLoginPage() 替换为适当的代码。

我不完全了解您的情况,但根据您的代码,您似乎可以更进一步,添加一个包含用户到站点的映射的表,并执行如下操作:

在公共模块中,添加下面的代码:

<System.Runtime.CompilerServices.Extension()> _
Public Function ManagesSite(target As System.Security.Principal.IPrincipal, siteName As String) As Boolean
    Return [ code here to look up whether this user can access the site specified ]
End Function 

然后你可以将前面的代码写成更符合逻辑的内容,例如:

If Not (User.IsInRole("SiteManager") AndAlso User.ManagesSite(Request.Url.Host)) Then _
    FormsAuthentication.RedirectToLoginPage()

I don't think you can do this via the current configs allowed in web.config. What you could do though is something like the following... as the very first line in your Page_Load event for the page in question, use the following code (VB):

If Not (User.IsInRole("Role1") AndAlso User.IsInRole("Role2")) Then _
    FormsAuthentication.RedirectToLoginPage()

This line of course is assuming you are using FormsAuthentication. If not, you would need to replace FormsAuthentication.RedirectToLoginPage() with the appropriate code depending on your authentication method.

I don't know your situation exactly, but based on your code, it looks like you could go one step further, and add a table with a mapping of users to sites, and do something like the following:

In a public module, add the following code:

<System.Runtime.CompilerServices.Extension()> _
Public Function ManagesSite(target As System.Security.Principal.IPrincipal, siteName As String) As Boolean
    Return [ code here to look up whether this user can access the site specified ]
End Function 

Then you can write the previous code as something more logical, such as:

If Not (User.IsInRole("SiteManager") AndAlso User.ManagesSite(Request.Url.Host)) Then _
    FormsAuthentication.RedirectToLoginPage()
世界等同你 2024-09-06 14:12:11

我通常用来解决这个问题的方法是在设置用户角色时,创建虚拟角色。因此,如果您希望仅允许学生管理员访问页面,并且用户同时具有学生和管理员角色,则可以添加新的学生管理员角色。

The method I usually use to solve this is when setting the user roles, create virtual roles. Therefore if the you wanted to only allow Student Administrators access to a page were a user has both Student and Administrator roles you could add a new StudentAdministrator role.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文