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

  • SEARCH
  • Home
  • 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 7606827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:30:24+00:00 2026-05-31T00:30:24+00:00

I have application with many ajax actions (implemented using JQuery.ajax), that returns JSON ot

  • 0

I have application with many ajax actions (implemented using JQuery.ajax), that returns JSON ot html. Some of them should be accessible only to authorized users, and I decorated them with [Authorize] attribute. For not ajax actions, if user not authorized – system redirects him to login page (that configured in web.config).

But this is not applicable for ajax actions, because if user was authorized – he load page, after that cookie expires and he is not authorized, and instead of html block, that should replace old, he get my login page inside block.

I know that I can solve this problem manually, for example remove [Authorize] attribute, and return special json/empty html, if user no authorized. But I dislike this solution, because I need to rewrite all my actions and ajax functions. I want global solution, that allow me not to rewrite my actions (may be custom authorize attribute, or some http unauthorized result custom handling).

I want to return status code, if request is ajax, and redirect to login page, if request isn’t ajax.

  • 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-31T00:30:26+00:00Added an answer on May 31, 2026 at 12:30 am

    Add an Application_EndRequest handler to your code in global.asax.cs that modifies any 302 error (redirect to login page) to an 401 (unauthorized) error for an AJAX request. This will allow you to simply leave your controller actions as they are and handle the redirect (you really can only have the user login again if they aren’t currently) via the client.

    protected void Application_EndRequest()
    {
        // Any AJAX request that ends in a redirect should get mapped to an unauthorized request
        // since it should only happen when the request is not authorized and gets automatically
        // redirected to the login page.
        var context = new HttpContextWrapper( Context );
        if (context.Response.StatusCode == 302 && context.Request.IsAjaxRequest())
        {
            context.Response.Clear();
            Context.Response.StatusCode = 401;
        }
    }
    

    Then in your _Layout.cshtml file add a global AJAX error handler that redirects to your login action when it gets a 401 response.

    <script type="text/javascript">
        $(function () {
            $(document).ajaxError(function (e, xhr, settings) {
                if (xhr.status == 401) {
                    location = '@Url.Action( "login", "account" )';
                }
            });
        });
    </script>
    

    You might also want to try some of the techniques outlined in Phil Haack’s blog, Prevent Forms Authentication Login Page Redirect When You Don’t Want It

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started using ajax/jQuery in our websites / application. There are many plugins
I'm using JSF 1.2 with Richfaces and Facelets. I have an application with many
I have an application that has many different types of objects that each persist
I have an application that writes many times to a formula/macro-laden workbook. It loops
I have an application that contains many controls on a panel, each with its
.Net MVC application also running Linq-to-SQL. I have seen many articles on grids that
I have a PHP application that calls web services APIs to get some objects
I have a page in my application that refreshes some content (a list of
Hopefully that title isn't too cryptic. What's happening is I have a jQuery AJAX
Here's what I have: A web application that runs in a single HTML page

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.