Asp.Net 核心 MVC 应用程序 Windows IIS 中的身份验证

Asp.Net core MVC application Windows Authentication in IIS

我的 Asp.Net Core mvc web 应用程序需要 Windows 身份验证。在开发中,在 IIS Express 上,由于此设置,一切正常

launchSettings.json

 "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:61545/",
      "sslPort": 0
    }
  }

部署到 IIS 时,我得到一个空白页面。对我网站的请求得到 500 错误代码。

我尝试将此配置添加到 Startup.cs,如 here 所述,但没有成功。

    services.Configure<IISOptions>(options => {
        options.ForwardWindowsAuthentication = true;
    });

当我直接在 IIS 中查看身份验证参数时,Windows 身份验证已激活。

我发现一些 post 在谈论一个名为 Microsoft.AspNetCore.Server.WebListener 的包,其他人在谈论实现自定义中间件。我无法想象这个基本功能需要那么多努力才能工作。我错过了什么吗?

launchSettings.json 文件仅供 VS 使用。当你发布你的应用程序时(或 运行 没有 VS) launchSettings.json 没有被使用。当您 运行 和 IIS/IISExpress 时,您只需要确保您的 web.config 具有正确的设置。在您的情况下,web.config 中的 forwardWindowsAuthToken 属性丢失或设置为 false。必须将其设置为 true 才能使 Windows 身份验证起作用。发布前的示例 web.config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
  </system.webServer>
</configuration>

您需要检查项目目录中的 web.config。这个设置对我很有帮助。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
    <security>
      <authentication>
        <anonymousAuthentication enabled="false" />
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
  </system.webServer>
</configuration>

对我来说,我必须添加行

services.AddAuthentication(IISDefaults.AuthenticationScheme);

在方法ConfigureServicesStartup.cs

我的应用允许 Windows 和匿名用户。

https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x#windows-authentication-httpsys--iisintegration