ASP.NET MVC Authorize Attribute does a 302 redirect when the user is not authorized

前端 未结 4 2057

MSDN explicitly says it should do 401 redirect, but I\'m getting a 302 redirect on FF, and this is causing problems in AJAX requests as the returned status is 200 (from the red

相关标签:
4条回答
  • 2021-02-04 09:46

    I really like this solution. By changing the 302 response on ajax requests to a 401 it allows you to setup your ajax on the client side to monitor any ajax request looking for a 401 and if it finds one to redirect to the login page. Very simple and effective.

    Global.asax:

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 302 &&
            Context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
        {
            Context.Response.Clear();
            Context.Response.StatusCode = 401;
        }
    }
    

    Client Side Code:

     $(function () {
          $.ajaxSetup({
            statusCode: {
              401: function () {
                location.href = '/Logon.aspx?ReturnUrl=' + location.pathname;
              }
            }
          });
        });
    
    0 讨论(0)
  • 2021-02-04 09:47

    If you are using a ASP.NET MVC 5 Web Application go to App_Start -> Startup.Auth.cs. Check if app.UseCookieAuthentication is enabled and see if CookieAuthenticationOptions is set to LoginPath = new PathString("/Login"), or similar. If you remove this parameter 401 will stop redirecting.

    Description for LoginPath:

    The LoginPath property informs the middleware that it should change an outgoing 401 Unauthorized status code into a 302 redirection onto the given login path. The current url which generated the 401 is added to the LoginPath as a query string parameter named by the ReturnUrlParameter. Once a request to the LoginPath grants a new SignIn identity, the ReturnUrlParameter value is used to redirect the browser back to the url which caused the original unauthorized status code. If the LoginPath is null or empty, the middleware will not look for 401 Unauthorized status codes, and it will not redirect automatically when a login occurs.

    0 讨论(0)
  • 2021-02-04 09:48

    The Authorize attribute does return a Http 401 Unauthorized response. Unfortunately, however if you have FormsAuthentication enabled, the 401 is intercepted by the FormsAuthenticationModule which then performs a redirect to the login page - which then returns a Http 200 (and the login page) back to your ajax request.

    The best alternative is to write your own authorization attribute, and then if you get an unauthenticated request that is also an Ajax request, return a different Http status code - say 403 - which is not caught by the formsAuthenticationModule and you can catch in your Ajax method.

    0 讨论(0)
  • 2021-02-04 09:58

    I implemented my own custom authorize attribute which inherited from AuthorizeAttribute and ran into the same problem.

    Then I found out that since .Net 4.5 there is a solution to this - you can suppress the redirect in the following way:

    context.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;
    

    Then the response will be a 401 - Unauthorized, along with the HTTP Basic authentication challenge.

    More info here

    0 讨论(0)
提交回复
热议问题