Jax*_*ian 3 authentication asp.net-mvc authorization asp.net-mvc-2
我想要做的是对动作处理程序进行两级角色检查.例如,要求用户至少属于以下组中的一个:SysAdmins,Managers 和至少一个以下组:HR,Payroll,Executive.
最初的猜测是,这可能是这样做的方法,但我认为不是:
[Authorize(Role="SysAdmins,Managers")]
[Authorize(Role="HR,Payroll,Executive")]
public ActionResult SomeAction()
{
[...]
}
Run Code Online (Sandbox Code Playgroud)
我是否需要将自己的自定义属性作为角色来接受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
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5651 次 |
| 最近记录: |