ASP.Net MVC 3 Redirect UnAuthorized User not to loginUrl

前端 未结 6 1590
庸人自扰
庸人自扰 2020-12-23 22:40

i have a project using ASP.Net MVC3 and using membership for roles. i use authorize in every controller. eg:

[Authorize(Roles = \"Administrator\")]
    publ         


        
6条回答
  •  甜味超标
    2020-12-23 23:14

    The code below helped and here is the reference in stackoverflow ASP.NET MVC 4 custom Authorize attribute - How to redirect unauthorized users to error page?

    public class CustomAuthorize: AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                base.HandleUnauthorizedRequest(filterContext);
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(new
                RouteValueDictionary(new{ controller = "Error", action = "AccessDenied" }));
            }
        }
    }
    

提交回复
热议问题