首页 文章

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

提问于
浏览
0

Problem

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

根据文档here,Windows身份验证应该在使用IIS托管时在Asp.Net Core 2.x中运行 .

Background

我正在按照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用户的标识可按预期方式使用 .

在完成迁移指南并修复所有构建问题后,我've created a 407005 profile under 407006 in the web project properties, setting the 407007 option to 407008 . I'只勾选"Enable Windows Authentication"然后浏览到该网站 . 尽管使用有效的域凭据登录, User.Identity.Name 仍为空 .

我在开始迁移过程之前安装了.Net Core 2.1 SDK,之前安装了.Net Core 1.0.1 SDK预览版 . 服务器正在运行带有IIS 7的Windows 2008 R2 .

What I've Tried

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

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

1 回答

  • 0

    Cause

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

    这可以通过在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或更高版本 .

    Solution 1 - Project Fix

    在配置本地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>
    

    Solution 2 - System-Wide Fix

    .Net downloads页面下载并安装最新的.Net Core Runtime . 运行时包括Windows主机包和所需的IIS模块 . 如果已安装SDK,则会包含Runtime,但是,可能仍需要再次安装Runtime来解决此问题(选择修复选项) .

相关问题