简体   繁体   English

如何让浏览器弹出默认用户认证提示?

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

I have a MVC application in C#.我在 C# 中有一个 MVC 应用程序。 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.然后应用程序应该使用 Active Directory 对其进行验证,并从 AD 获取有关用户的其他一些信息。

I have no issue in validating and getting the user information from AD if I have the username.如果我有用户名,我在验证和从 AD 获取用户信息方面没有问题。

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?所以我的问题是如何让浏览器提示这样的模态对话框以及如何访问用户的输入以使用我的 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.需要告知浏览器,它必须在后续的 HTTP 请求中提供基本身份验证。 To do that, you need to set the HTTP header:为此,您需要设置 HTTP header:

"WWW-Authenticate", "Basic"

This will inform the browser that the next request will have basic auth creds这将通知浏览器下一个请求将具有基本的身份验证凭据

Here is a C# example:这是一个 C# 示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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