Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3395502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:17:51+00:00 2026-05-18T04:17:51+00:00

I have a view that loads a partial view from a controller action using

  • 0

I have a view that loads a partial view from a controller action using jQuery. The controller action is decorated with the Authorize attribute and if the user has timed out when that action is called intead of being redirected to the proper LogOn page, the LogOn page gets inserted in the view where the partial view would have gone.

There is another post here that describes a solution using jQuery but unfortunately it doesn’t include any code samples which is what I need because I’m not very familiar with jQuery (and new to web development) and I need to get this issue resolved as soon as I can.

All I want to do is redirect the user to the proper LogOn page which sounds pretty straight forward to me but so far it doesn’t seem to be. If jQuery is the way to go, could someone please post a code sample, that would really help me out.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:17:51+00:00Added an answer on May 18, 2026 at 4:17 am

    Solution 1, return response indicating error

    Pro ASP.NET MVC 2 Framework offers one solution for this

    If an Ajax request is
    denied authorization, then usually
    you don’t want to return an HTTP
    redirection to the login page, because
    your client-side code is not
    expecting that and may do something
    unwanted such as injecting the entire
    login page into the middle of
    whatever page the user is on. Instead,
    you’ll want to send back a more useful
    signal to the client-side code,
    perhaps in JSON format, to explain
    that the request was not authorized.
    You could implement this as follows:

    public class EnhancedAuthorizeAttribute : AuthorizeAttribute 
    { 
        protected override void HandleUnauthorizedRequest(AuthorizationContext context) 
        { 
            if (context.HttpContext.Request.IsAjaxRequest()) { 
                UrlHelper urlHelper = new UrlHelper(context.RequestContext); 
                context.Result = new JsonResult { 
                    Data = new { 
                        Error = "NotAuthorized", 
                        LogOnUrl = urlHelper.Action("LogOn", "Account") 
                    }, 
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet 
                }; 
            } 
            else 
                base.HandleUnauthorizedRequest(context); 
        } 
    } 
    

    You’d then get back a JSON object from the controller {'Error': 'NotAuthorized', 'LogonUrl': '...'} which you could then use to redirect the user.

    And alternative response, if you’re expecting HTML, could be to return a plain string, like NotAuthorized:<your_url> and check if the response matches this pattern.

    Solution 2, return and handle 401 Unautorized status code

    As this has to be checked on every ajax request’s success callback, this gets quite tedious. It would be nice to be able to catch this case globally. The best solution would be to return a 401 Unauthorized status code from the server and use jQuery’s .ajaxError to catch it, but this is problematic on IIS/ASP.NET, since it’s deadly committed to redirecting you to the login page if the response has this statuscode. As long as the <authentication> tag is present in web.config this redirection will happen [1] if you don’t do something about it.

    So let’s do something about it. This hacky way seemed nice. In your global.asax.cs

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 302 && Context.Request.RequestContext.HttpContext.Request.IsAjaxRequest())
        {
            Context.Response.Clear();
            Context.Response.StatusCode = 401;
        }
    }
    

    (I’d love to hear it if anyone knows of any better methods to return a 401 status code.)

    By doing this, you’re preventing the default behaviour of redirecting to the logon page when the request is an ajax request. You can therefore use the default AuthorizeAttribute as it is, since Application_EndRequest takes care of the rest.

    Now, in the jQuery code we’ll use the .ajaxError function to catch the error. This is a global ajax event handler, meaning it will intercept every error made by any ajax call. It can be attached to any element – in my example I’ve just chosen body cause its always present

    $("body").ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError) {
        if (XMLHttpRequest.status == 401) {
            alert("unauthorized");
        }
    });
    

    This way you get your redirection logic centralized in one place, instead of having to check it on every damn ajax request success callback

    Solution 3, return custom header

    This answer offers an alternative solution. It uses the same kind of global event handler, but skips the hacky bits. It adds a custom header to the page if you’re not authenticated and the request is an ajax request, and checks for this header on each .ajaxComplete. Note that the method provided in the linked answer is unsecure, as it will return the original view with possibly sensitive content and rely on javascript to redirect the user away from it. With a bit of modification it’s quite awesome as it doesn’t require any hacks and the logic can be centralized to one callback function. The only downside as I can see is that it doens’t reply with the semantically correct status code, as it’s actually not 200 OK because you’re 401 Unauthorized

    We can bake this into another custom EnhancedAuthorizationAttribute

    public class EnhancedAuthorizeAttribute : AuthorizeAttribute
    {
            protected override void HandleUnauthorizedRequest(AuthorizationContext context)
            {
                if (context.HttpContext.Request.IsAjaxRequest()) 
                {
                    context.HttpContext.Response.AddHeader("REQUIRES_AUTH", "1");
                    context.Result = new EmptyResult();
                }
                else
                {
                    base.HandleUnauthorizedRequest(context);
                }
            }
        }
    }
    

    Now, every time an ajax request completes, you check for this header:

    $('body').ajaxComplete(function(event,request,settings){
        if (request.getResponseHeader('REQUIRES_AUTH') === '1'){
            alert("unauthorized");
        };
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when
I have a view that has a list of jobs in it, with data
Image i have a view that is cached with the OutputCache attribute but i
I have a view user control that can post form. This control can be
I have a view using a master page that contains some javascript that needs
Short: Is there a way to have a route-definition that will pass the CONTROLLER/ACTION
I have a partial view that displays a search options like search by category,
I have a partial view that I want to basically take care of itself
I have a view that I want to add some custom drawing to. I
I have a View that can vary significantly, depending on the 'mode' a particular

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.