我在类方法上指定两个单独的授权属性时遇到问题:如果两个属性中的任何一个为真,则允许用户访问。

Athorization 类如下所示:

[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
public class AuthAttribute : AuthorizeAttribute {
. . .

和行动:
[Auth(Roles = AuthRole.SuperAdministrator)]
[Auth(Roles = AuthRole.Administrator, Module = ModuleID.SomeModule)]
public ActionResult Index() {
    return View(GetIndexViewModel());
}

有没有办法解决这个问题,或者我需要重新考虑我的方法吗?

这将在 MVC2 中运行。

最佳答案

MVC 处理多个 AuthorizeAttribute 实例,就好像它们与 AND 连接一样。如果您想要 OR 行为,您将需要实现自己的检查逻辑。最好实现 AuthAttribute 以承担多个角色并使用 OR 逻辑执行自己的检查。

另一种解决方案是使用标准 AuthorizeAttribute 并实现自定义 IPrincipal 将实现 bool IsInRole(string role) 方法以提供“或”行为。

一个例子在这里:
https://stackoverflow.com/a/10754108/449906

关于c# - 方法上的多个授权属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17272422/

10-14 16:38
查看更多