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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:21:17+00:00 2026-05-31T11:21:17+00:00

We have an internal system where users can authenticate using Windows authentication but we

  • 0

We have an internal system where users can authenticate using Windows authentication but we want to include some custom content in the 401 repsonse that takes the user to a custom username/password authentication page if the cancel off the Windows authentication dialog.

When an unauthenticated user accesses a page, Windows authentication responds with a 401 Unauthorized response, with the headers

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

This prompts the browser to show a dialog asking for Windows credentials. If the user enters their credentials, there is another handshake request-response, and then the user is authenticated and the requested page is shown.

If the user cancels the dialog, the browser displays the content of the original 401 Unauthorized reponse.
We use Application_EndRequest in the Global.asax to return some custom content here which takes the user to our custom login system.

    /// <summary>
    /// If we are about to send the NTLM authenticate headers, include this content. Then, if the user cancels off the
    /// windows auth dialog, they will be presented with this page and redirected to the username / password login screen
    /// </summary>
    void Application_EndRequest(object sender, EventArgs e)
    {
        Logger.DebugFormat("in Application_EndRequest. Response.StatusCode: {0}", Response.StatusCode);

        if (Response.StatusCode == 401)
        {
            Response.ContentType = "text/html";

            var redirectUrl = string.Format("https://mycompany.com/SSO/Login?encryptedSessionRequestId={1}",
                HttpUtility.UrlEncode(Request.QueryString["encryptedSessionRequestId"]));

            var text = File.ReadAllText(Server.MapPath("~/UnauthorizedError.html"));
            text = text.Replace("[USERNAME_PASSWORD_LOGON_REDIRECT]", redirectUrl);


            Response.Write(text);

            Logger.Debug("Done writing response");
            Response.End();
        }
    }

UnauthorizedError.html contains the following script which forwards the user on

<script language="javascript" type="text/javascript">
    window.location = "[USERNAME_PASSWORD_LOGON_REDIRECT]";
</script>

When running locally in (Win7/IIS7.5), this works great, we use Response.Write() to send our content and the user is able to see it when they cancel the dialog.

But when accessing the site remotely, e.g. when it running in our development environment, although we can see from the logs that Application_EndRequest is being called and our content written to the response, it is overridden at some point and all that reaches the browser is the authentication headers and the text You do not have permission to view this directory or page.

1) How can we prevent this content being overwritten?

2) Where in the pipeline might this be happening?

3) Does anyone have any suggestions of another way to implement this behaviour, e.g. with an HTTP module?

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-31T11:21:19+00:00Added an answer on May 31, 2026 at 11:21 am

    I wasted a lot to solve this problem but there isn’t direct solution.
    It because win auth works on iis not site level and you can’t control how to auth current request.
    There is several hacky ways to use redirection on different login pages depending on ip.

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

Sidebar

Related Questions

I have a class property exposing an internal IList<> through System.Collections.ObjectModel.ReadOnlyCollection<> How can I
I have built a very simple internal messaging system inside my application, i want
I'm trying to wrap up a custom authentication system for internal use for a
I have a internal website that users log into. This data is saved as
I'm having a system where users can input their purchased domain into their profile,
I have a site where users register for an account. I have an internal
We have an application that uses various system tray icons to communicate with users.
I'm trying to build a Windows installer using Nullsoft Install System that requires installation
Hi I'm using System.Net.Mail to send some HTML formatted emails. What is the correct
I'm designing a system where users will be able to register and afterward authenticate

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.