asp.net-core-2.0 - 使用 Windows 身份验证的 Asp.Net Core 2.x Web 应用程序中的 User.Identity.Name 为 null

标签 asp.net-core-2.0 asp.net-core-2.1

问题

在 IIS 后面托管 Asp.Net Core 2.0 或 2.1 Web 应用程序且 Windows 身份验证设置为 true,匿名身份验证设置为 false 时,User.Identity.Name 属性为 null,并且 User.Identity.IsAuthenticated 为 false。

根据文档here ,当使用 IIS 托管时,Windows 身份验证应该只能在 Asp.Net Core 2.x 中工作。

背景

我正在按照 Migrate from ASP.NET MVC to ASP.NET Core MVC guide 将 Asp.Net 4.x MVC 应用程序迁移到 Asp.Net Core .

我正在服务器上测试该站点,该服务器当前托管使用 Windows 身份验证的 Asp.Net 4.x MVC 应用程序,没有任何问题。 Windows 用户的身份按预期可用。

完成迁移指南并修复所有构建问题后,我在 Web 项目属性中的“调试”下创建了一个“本地 IIS”配置文件,并将“启动”选项设置为“IIS”。我只勾选了“启用 Windows 身份验证”,然后浏览到该网站。尽管使用有效的域凭据登录,User.Identity.Name 仍然为空。

我在开始迁移过程之前安装了 .Net Core 2.1 SDK,并且之前安装了 .Net Core 1.0.1 SDK Preview。服务器运行的是带有 IIS 7 的 Windows 2008 R2。

我尝试过的

为了确保在迁移过程中没有引入此问题,我使用 MVC 模板并针对 .NET Framework 4.7.2 创建了一个新的 ASP.NET Core Web 应用程序。我在选择模板时配置了“Windows 身份验证”。在确认使用 IIS Express 时 Windows 身份验证有效后,我按上述方式配置了本地 IIS。当浏览到IIS下时,导航栏右上角显示“Hello, !”。我使用 Asp.Net Core SDK 2.0 和 2.1 中的模板尝试了此操作。

我遵循了各种指南和 Stackoverflow 的答案,所有这些都与在 Asp.Net Core 应用程序本身中配置 Windows 身份验证有关。结果要么没有变化,要么连续出现登录提示,从不接受有效的用户名和密码。看来这些解决方案可能是针对旧版本的框架或开发人员尝试组合多种身份验证方法的场景编写的。

最佳答案

原因

服务器安装了过时的 .Net Core 2.0 IIS 模块,默认情况下不转发 Windows 身份验证详细信息。此问题描述如下:.Net Core 2.0 Project With Windows Authentication Fail When Published to IIS 。安装最新的 .Net Core 2.0 运行时应该可以解决此问题。

可以通过在 PowerShell 中运行以下命令来验证这一点:

(Get-Item $env:SystemDrive\Windows\System32\inetsrv\aspnetcore.dll).VersionInfo

这给出了以下输出:

ProductVersion   FileVersion      FileName
--------------   -----------      --------
7.1.1967.0       7.1.1967.0       C:\Windows\System32\inetsrv\aspnetcore.dll

服务器需要版本 7.1.1972.0 或更高版本。

解决方案 1 - 项目修复

在配置本地 IIS 时 Visual Studio 2017 生成的 web.config 中,添加 forwardWindowsAuthToken="true"作为 <aspNetCore> 的属性元素:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="bin\IISSupport\VSIISExeLauncher.exe" arguments="-argFile IISExeLauncherArgs.txt" stdoutLogEnabled="false" forwardWindowsAuthToken="true" />
    </system.webServer>
  </location>
  <system.web>
    <authentication mode="Windows" />
  </system.web>
</configuration>

解决方案 2 - 系统范围修复

.Net downloads 下载并安装最新的 .Net Core 运行时页。运行时包括 Windows 托管 bundle 和所需的 IIS 模块。如果已经安装了 SDK,这将包括运行时,但是,运行时可能仍需要自行再次安装以修复此问题(选择修复选项)。

关于asp.net-core-2.0 - 使用 Windows 身份验证的 Asp.Net Core 2.x Web 应用程序中的 User.Identity.Name 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52185514/

相关文章:

jwt - AAD Jwt token 在本地主机上验证但不在服务器上验证

c# - 如何在 .Net Core 2.1 中为暂存/生产配置 ssl

c# - C# 的多个 API 模型

razor - 从 asp.net core 中的中间件返回现有的 Razor View

asp.net-core - Asp.net Core持久化认证——自定义Cookie认证

ssl - 如何在 ASP.NET Core 2.x 中将 HTTPS/SSL 与 Kestrel 一起使用?

c# - 在 ViewComponent : This async method lacks 'await' operators and will run synchronously

asp.net-core - 获取错误 : Entity type 'Course' is defined with a single key property,,但 2 个值已传递给 'DbSet.Find' 方法

c# - CookieAuthenticationOptions.LogoutPath 属性在 ASP.NET Core 2.1 中有什么作用?

asp.net-core-mvc - 类型 'IdentityRole' 在未引用的程序集中定义