为 asp.net 核心网站实施 windows 身份验证

Implementing windows authentication for asp.net core website

我正在尝试向新的 asp.net 核心 2.2 版网站添加 windows 身份验证。创建网站时 `1) 我已将身份验证模式更改为 "Windows authentication"。在 launchsettings.json

中验证
"windowsAuthentication" = true, "AnonymousAuthentication": false,

2) 在 ConfigServices 下的 Startup.cs 文件中添加了以下行。

services.AddAuthentication(IISDefaults.AuthenticationScheme);

3) 我目前在我的本地 运行 所以在我的本地计算机中,启用 -> 打开或关闭 Windows 功能 -> Internet 信息服务 -> 安全 -> "Windows Authentication"

本地系统上的问题:1) windows 不显示弹出窗口以输入用户名和密码 2) 我需要显示 Windows 用户名和目录详细信息(部门例如:IT、基于用户名的销售部门) 如何实现它。 任何建议都会有所帮助。

1) windows pop up is not displayed to enter username and password

如果您使用 IIS 来托管 Web 应用程序,您应该使用 IIS 管理控制台启用 windows 身份验证或修改 web.config 文件。

据我所知 launchsettings.json 具有以下功能:

  • 仅在本地开发机器上使用。
  • 未部署。
  • 包含配置文件设置。

这意味着 windows 身份验证设置将不会在部署后在 IIS 中启用。您应该使用 IIS 管理控制台启用它。

开启认证:

禁用匿名身份验证并启用windows身份验证

或者您可以在 web.config

中添加以下设置
<system.webServer>
    <security>
        <authentication>
            <anonymousAuthentication enabled="false" />
            <windowsAuthentication enabled="true" />
        </authentication>
    </security>
</system.webServer>

如果您想使用 kestrel 而不是 IIS 来托管应用程序,您应该在您的应用程序中使用 HTTP.SYS。更多使用方法可以参考这个article.

2) I need to display Windows User Name and Directory details(Department eg: IT, Sales department based on username)

如果你想获取用户名,我建议你可以尝试使用User.Identity.Name

如果你想得到当前用户的部门,我建议你可以尝试使用这个