我有一个有很多 Action 的 Controller 。除了一项操作外,所有用户都应该只能访问某些用户:
[Authorize(Roles = "Admin")]
public class SecretsController : Controller
{
[Authorize]
public ActionResult Index()
{
return View(...);
}
...
}
偶
[Authorize (Roles = null)]
不起作用。方法属性将被忽略!我怎么能得到一个 Action 的异常(exception)?赞 AllowAnonymous
允许它但对登录用户可见?
最佳答案
您可以使用 OverrideAuthorization
属性就像我在下面的代码中所做的那样:
[Authorize(Roles = "Admin")]
public class SecretsController : Controller
{
[OverrideAuthorization]
[Authorize()]
public ActionResult Index()
{
return View(...);
}
...
}
与
[OverrideAuthorization]
ASP.Net MVC5 附带的,你告诉你的 Index
覆盖/忽略在 Controller 级别定义的授权规则的操作。通过这样做,您在
SecretsController
中定义的所有操作仅对 可见管理员 角色除外 Index
操作仅对经过身份验证的用户可见,即使他们不在 管理员 角色。
关于c# - 对 AuthorizeAttribute 进行异常(exception)处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35685147/