简体   繁体   中英

How to make the browser prompt for default user authentication pop up?

I have a MVC application in C#. I need it to prompt the default browser authentication login popup as below image.

在此处输入图像描述

Then the application supposed to validate it with the Active Directory and get some other information regarding the user from the AD.

I have no issue in validating and getting the user information from AD if I have the username.

So my question is how can I make the browser prompt such modal dialog and how can I access the user's input to validate it with my AD?

Here is what I have so far. The code will only get the username from the logon user and won't prompt the pop up:

private string GetUsernameFromLogon()
    {
        string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
        int index = userName.LastIndexOf("\\");
        if (index > 0)
            return userName.Substring(index+1);
        else
            return null;
    }

The browser needs to be told that it must provide basic authentication in subsequent HTTP requests. To do that, you need to set the HTTP header:

"WWW-Authenticate", "Basic"

This will inform the browser that the next request will have basic auth creds

Here is a C# example:

Response.Clear();
Response.StatusCode = (Int32)HttpStatusCode.Unauthorized;
Response.AddHeader("WWW-Authenticate", "Basic");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM