asp.net-mvc – 使用MVC的AuthorizeAttribute和多组角色?

我想要做的是对动作处理程序进行两级角色检查.例如,要求用户至少属于以下组之一:SysAdmins,Managers AND至少在以下一个组中:HR,Payroll,Executive.

最初的猜测是,这可能是这样做的方法,但我认为不是:

[Authorize(Role="SysAdmins,Managers")]
[Authorize(Role="HR,Executive")]
public ActionResult SomeAction()
{
    [...]
}

我是否需要将自己的自定义属性作为角色来接受Role1和Role2或类似的东西?或者有更简单/更好的方法吗?

解决方法

你需要自己的属性.这是我的:
public class AuthorizationAttribute : ActionFilterattribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var portalModel = ContextCache<PortalModel>.Get(ContextCache.PortalModelSessionCache);

        var requestedController = filterContext.RouteData.GetrequiredString("controller");
        var requestedAction = filterContext.RouteData.GetrequiredString("action");

        var operation = string.Format("/{0}/{1}",requestedController,requestedAction);

        var authorizationService = IoC.Container.Resolve<IAuthorizationService>();

        if (!authorizationService.IsAllowed(AccountController.GetUserFromSession(),operation))
        {
            filterContext.Controller.ViewData["Message"] = string.Format("You are not authorized to perform operation: {0}",operation);
            filterContext.HttpContext.Response.Redirect("/Error/NoAccess");
        }
        else
        {
        }

    }

}

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....