代码之家  ›  专栏  ›  技术社区  ›  Andy Vaal

User.Identity.Name中为空Asp.Net使用Windows身份验证的核心2.x web应用程序

  •  0
  • Andy Vaal  · 技术社区  · 7 年前

    问题

    User.Identity.Name 属性为null,并且 User.Identity.IsAuthenticated

    根据文件 here ,Windows身份验证应该只在Asp.Net使用IIS托管时使用Core 2.x。

    背景

    我正在迁移一个Asp.Net4.x MVC应用程序转移到Asp.Net核心遵循 Migrate from ASP.NET MVC to ASP.NET Core MVC guide

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

    在完成了迁移指南并解决了所有生成问题之后,我在web项目属性的“Debug”下创建了一个“Local IIS”概要文件,将“Launch”选项设置为“IIS”。我只勾选了“启用Windows身份验证”,然后浏览到网站。尽管使用有效的域凭据登录, User.Identity.Name 仍为空。

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

    我试过的

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Andy Vaal    7 年前

    原因

    .Net Core 2.0 Project With Windows Authentication Fail When Published to IIS . 安装最新的.Net Core 2.0运行时应该可以解决此问题。

    (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
    

    解决方案1-项目修复

    在web.config文件由Visual Studio 2017在配置本地IIS时生成,添加 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 第页。运行时包括Windows主机包和必需的IIS模块。如果SDK已经安装,这将包括运行时,但是,运行时可能仍然需要自己重新安装以修复此问题(选择修复选项)。