ASP.NET MVC - How to show unauthorized error on login page?

后端 未结 7 2033
花落未央
花落未央 2020-11-28 02:58

In my ASP.NET MVC app, I have most controllers decorated with

[Authorize(Roles=\"SomeGroup\")]

When a user is not authorized to access som

相关标签:
7条回答
  • 2020-11-28 03:43

    I like what Brian Vander Plaats posted, just added few improvements:

    /// <summary>
    /// Authorize or redirect to an unauthorized MVC action if the user does not have the required roles
    /// (an unauthenticated user will be redirected to the defualt sign in action)
    /// <para>Decorate an action or a controller like this [AuthorizeAndRedirect(Roles = "RoleName")]</para>
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class AuthorizeOrRedirectAttribute : System.Web.Mvc.AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            base.HandleUnauthorizedRequest(filterContext);
    
            if (filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
            {
                var routeData = new RouteData();
                routeData.Values.Add("controller", "Error");
                routeData.Values.Add("action", "Unauthorized");
                filterContext.Result = new RedirectToRouteResult(routeData.Values);
            }
        }
    }
    
    /// <summary>
    /// Authorize or redirect to an unauthorized API action if the user does not have the required roles
    /// (an unauthenticated user will be redirected to the defualt sign in action)
    /// <para>Decorate an action or a controller like this [AuthorizeAndRedirect(Roles = "RoleName")]</para>
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public class AuthorizeOrRedirectApiFilterAttribute : System.Web.Http.AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
        {
            base.HandleUnauthorizedRequest(actionContext);
    
            if (actionContext.RequestContext.Principal.Identity.IsAuthenticated)
            {
                actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题