asp.net-mvc – 使用MVC的AuthorizeAttribute和多组角色?
发布时间:2020-12-16 00:16:07 所属栏目:asp.Net 来源:网络整理
导读:我想要做的是对动作处理程序进行两级角色检查.例如,要求用户至少属于以下组之一:SysAdmins,Managers AND至少在以下一个组中:HR,Payroll,Executive. 最初的猜测是,这可能是这样做的方法,但我认为不是: [Authorize(Role="SysAdmins,Managers")][Authorize(R
我想要做的是对动作处理程序进行两级角色检查.例如,要求用户至少属于以下组之一: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 { } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- asp.net – 在发布Web应用程序时如何包含未引用的
- asp.net-mvc – 使用WCF进行ASP表单身份验证?
- asp.net – 为什么HttpUtility.UrlEncode(HttpUt
- asp.net – 当主机名中有下划线时,表单身份验证无
- asp.net-web-api – 从ASP.NET Web API ASP.NET
- asp.net-mvc – 表单身份验证cookie未过期
- asp.net – Context.Response.End()和Thread正在
- Windbg程序调试系列-索引篇
- Task.Factory.StartNew在ASP.Net MVC中有帮助或有
- asp.net – 如何从ajax调用web api Controller
热点阅读