将 MVC 的 AuthorizeAttribute 与多组角色一起使用?
我想做的是对操作处理程序进行两级角色检查。例如,要求用户至少属于以下组之一:系统管理员、经理并且至少属于以下组之一:HR、薪资、主管。
最初的猜测是这可能是执行此操作的方法,但我不认为是这样:
[Authorize(Role="SysAdmins,Managers")]
[Authorize(Role="HR,Payroll,Executive")]
public ActionResult SomeAction()
{
[...]
}
我是否需要使用自己的自定义属性来接受 Role1 和 Role2 或类似的内容?或者有更简单/更好的方法来做到这一点?
What I want to do is a two-level role check on an action handler. For example, Require that the users is in at least one of the following groups: SysAdmins, Managers AND in at least one of the following groups: HR, Payroll, Executive.
Initial guess was that this might be the way to do this but I don't think it is:
[Authorize(Role="SysAdmins,Managers")]
[Authorize(Role="HR,Payroll,Executive")]
public ActionResult SomeAction()
{
[...]
}
Do I need to role my own custom Attribute to take in Role1 and Role2 or something like that? Or is there an easier/better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要你自己的属性。这是我的:
You'll need your own attribute. Here's mine:
没有内置的方法可以做你想做的事。您要么必须编写自己的新属性,要么在操作中添加检查,如果用户的角色未通过检查,则返回 UnauthorizedActionResult。
There is no built-in way to do what you want. You will either have to write your own new attribute, or add the check inside the action and return an UnauthorizedActionResult if the user's role fails your checks.