0

I want to create ASP.NET web page which will prompt me basic authentication popup where in I will enter credentials. I tried adding below lines of code in PreInit and PreLoad event handlers but still it is not showing me basic authentication popup.

    protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            Response.Headers.Add("WWW-Authenticate", "Basic");
        }



protected override void OnPreLoad(EventArgs e)
{
    base.OnPreLoad(e);
    Response.Headers.Add("WWW-Authenticate", "Basic");
    Response.AppendHeader("WWW-Authenticate", "Basic");
}

Can you please help me with this ? Thanks in advance.

1
  • as the answers below note, you're better of doing this via IIS instead of trying to re-implement the same thing in your application Commented Mar 4, 2019 at 19:49

1 Answer 1

1

You would typically do this in IIS. See: https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/authentication/basicauthentication

The short version:

  • ensure that the basic authentication feature is enabled on your server
  • disable anonymous authentication in your site and enable basic authentication
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. It worked for me. Marking it as answer. Can you tell me how to configure username and password and URL to be redirected after successful authentication ?
On the page that's secured by basic authentication, put a control or other mechanism to redirect to where you want the user to go to. You won't have their username / password, but if the target page is also secured with basic authentication to the same account, they will be able to access it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.