【问题标题】:User.Identity.Name is null in Asp.Net Core 2.x web application using Windows AuthenticationUser.Identity.Name 在使用 Windows 身份验证的 Asp.Net Core 2.x Web 应用程序中为空
【发布时间】:2019-02-10 14:55:29
【问题描述】:

问题

在 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。

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

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

我在开始迁移过程之前安装了 .Net Core 2.1 SDK,并且之前安装了 .Net Core 1.0.1 SDK 预览版。服务器正在运行带有 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 身份验证有关。结果要么没有变化,要么连续的登录提示从未接受有效的用户名和密码。看来这些解决方案可能是针对较旧版本的框架或开发人员尝试组合多种身份验证方法的场景编写的。

【问题讨论】:

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


    【解决方案1】:

    原因

    服务器安装了过时的 .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 - 项目修复

    在Visual Studio 2017配置本地IIS时生成的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 Runtime。运行时包括 Windows Hosting Bundle 和所需的 IIS 模块。如果 SDK 已经安装,这将包括 Runtime,但是,Runtime 可能仍需要自行重新安装以修复此问题(选择修复选项)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2017-08-31
      相关资源
      最近更新 更多