我试图阻止特定角色(比如 RoleA)中的特定用户访问特定操作。允许匿名用户访问,但不允许 RoleA 中的用户访问该操作。
所以我做了这样的事情:
[AllowAnonymous]
[CustomAuthorize(Roles="RoleB,RoleC")]
public ActionResult MyAction(){
//irrelevant
}
但是,
CustomAuthorize
当 [AllowAnonymous]
时, Action 过滤器永远不会被命中存在。[AllowAnonymous]
也是如此覆盖 [CustomAuthorize]
?
最佳答案
回答问题(有点晚,但可能对某人有用):
AllowAnonymous 有以下描述:
Represents an attribute that marks controllers and actions to skip the AuthorizeAttribute during authorization.
因此,将它与授权属性一起添加将导致授权代码根本不运行。
这也有这样的效果,如果您将其作为一个属性添加到整个 Controller 上(即在类级别),则向该 Controller 上的操作添加单独的 Authorize 属性将不起作用。
关于asp.net - AllowAnonymous 是否覆盖 AuthorizeAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22955272/