【问题标题】:HttpContext.User.Identity not set when not using IIS Express不使用 IIS Express 时未设置 HttpContext.User.Identity
【发布时间】:2017-08-31 18:31:39
【问题描述】:

我使用 Visual Studio 2017 和 ASP.NET Core 1.2 做了一个玩具项目,当我的项目作为独立应用程序或在 IIS 10 (WS2016) 中执行时,我在检索用户的 WindowsIdentity 时遇到了一些问题,但可以正常使用IIS Express。

我一直使用同一个导航器,所以我相信它的配置是正确的。

我做了一个中间件:

public async Task Invoke(HttpContext context) {
    var identity = context.User.Identity;

    foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(identity)) {
        string name = descriptor.Name;
        object value = descriptor.GetValue(identity);
        _logger.LogDebug("{0}={1}", name, value);
        // Displays IsAuthenticated=False, Name=(null), ...
    }

    var contextIdentity = context.User.Identity as WindowsIdentity;
    // Here contextIdentity is null
    ...

我的 launchSettings.json 看起来像:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
      "applicationUrl": "http://localhost:1028/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "AVVC.UI.ASP.SMP.Proto.Secur.API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

在我的 .csproj 中,我为 web.config 添加了以下规则:

  <ItemGroup>
    <Content Update="web.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <forwardWindowsAuthToken>True</forwardWindowsAuthToken>
    </Content>
  </ItemGroup>

并相应生成 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=".\blehbleh.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />
  </system.webServer>
</configuration>

【问题讨论】:

    标签: asp.net-core single-sign-on windows-authentication


    【解决方案1】:

    它不适用于独立应用程序,因为 launchSettings.json 文件,您有 iisSettings 部分仅在您从 Visual Studio 运行应用程序时使用。

    此 json 文件包含与每个调试配置文件关联的项目特定设置,Visual Studio 配置为用于启动应用程序,包括应使用的任何环境变量。


    要解决 IIS 10 的第二个问题,您需要为您的 IIS 站点设置 Windows 身份验证。来自Configure Windows Authentication in ASP.NET Core 中的“自定义身份验证”部分:

    • 禁用匿名身份验证并启用 Windows 身份验证。

    还有相关的 SO:dotnet run - angular - windows authentication - not authenticated

    【讨论】:

    • 所以它不能独立工作? (我不明白如何......)
    • @willll 不完全是。可以为使用 IIS 或 WebListener 托管的 ASP.NET Core 应用程序配置 Windows 身份验证,因此您可以使用 WebListener(而不是默认使用的 Kestrel)来支持自托管方案(但仅在 Windows 上)。查看链接文档以获取设置说明。
    • 当我添加 services.Configure(options => { options.ForwardWindowsAuthentication = true; });在配置服务中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    相关资源
    最近更新 更多