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 498495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:53:21+00:00 2026-05-13T05:53:21+00:00

We have an ASP.NET app protected by forms authentication. The app uses MS AJAX

  • 0

We have an ASP.NET app protected by forms authentication. The app uses MS AJAX heavily to call its web-services.

When the forms authentication times out, and a GET-request happens – all is fine (the user is redirected to a login page).

BUT when the forms authentication times out and a POST-request happens (ajax) – no redirect happens, instead the app returns “401 unathorized” and the browser prompts for username and password (not a login form, but a browsers built-in dialog). Of course entering ANY username/password never helps.

How do I handle this?

UPDATE: After looking with firebug, I just found out that regular POST requests redirect to login fine, it’s only web-service calls that throw “401 Unauthorizes”.
The difference between a regular request and web-service is URL. Which is “page.aspx” for a regular post-request and “service.asmx/MethodName” for webservices…

  • 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-13T05:53:22+00:00Added an answer on May 13, 2026 at 5:53 am

    Ok, answering my own questin.

    After looking into this issue and researching a bit more I found that when a web-app is protected by Forms-Authentication and the user is not authenticated, this is what happens:

    • If it’s a GET-request – the user is
      redirected to the login page.
    • If it’s a POST-request to a page – the user is
      redirected to the login page.
    • If it’s a POST-request to a web-service – the
      user gets 401-unauthorized

    Thats how ASP.NET works

    And if a web-service is called by AJAX (xmlHttpRequest object) and returns 401 – of course the browser shows a pop-up login box.

    Now, what should you do is add some code to Application_PostAuthenticateRequest that will prevent throwing 401 for webservices.

    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
        if (Request.RequestType == "POST" //if its POST
            && !User.Identity.IsAuthenticated //if user NOT authed
            && !HasAnonymousAccess(Context) //if it's not the login page
            )
        {
            //lets get the auth type
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            SystemWebSectionGroup grp = (SystemWebSectionGroup)config.GetSectionGroup("system.web");
            AuthenticationSection auth = grp.Authentication;
            //if it FORMS auth
            if(auth.Mode== AuthenticationMode.Forms)
            {
    
                //then redirect... this redirect won't work for AJAX cause xmlHttpRequest can't handle redirects, but anyway...
                Response.Redirect(FormsAuthentication.LoginUrl, true);
                Response.End();
    
            }
        }
    }
    public static bool HasAnonymousAccess(HttpContext context)
    {
        return UrlAuthorizationModule.CheckUrlAccessForPrincipal(
            context.Request.Path,
            new GenericPrincipal(new GenericIdentity(string.Empty), null),
            context.Request.HttpMethod);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net update web app. Users go the the page, hit the
I'm writing an app using asp.net-mvc deploying to iis6. I'm using forms authentication. Usually
I have an ASP.NET web app (C#) where I get some information from a
I have an asp.net app and I am trying to save a text file
I have an ASP.NET app running in IIS that is hosting files all great
I have a main asp.net app, which is written in asp.net 1.1. Runnning underneath
I have an asp.net mvc app running on a local iis website that is
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
I have a ASP.net 2.0 app and I have made some changes the the
I have a multi-user ASP.NET app running against SQL Server and want to have

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.