首页 > 解决方案 > Asp.Net Core Windows 身份验证在 IIS 中不起作用

问题描述

context?.User?.Identity?.Name当启用 Windows 身份验证并在 IIS Express 或 IIS 中运行时,Asp.Net Core 似乎无法从调用中识别用户。

期望行为:在 IIS 和/或 IIS Express 中同时启用 Windows 身份验证和匿名身份验证,Asp.Net Core 应自动识别 windows 用户。

实际行为:当我在 IIS 或 IIS Express 中同时启用 Windows 和匿名身份验证时,用户名为空。当我禁用匿名身份验证或调用HttpContext.ChallengeAsync(IISDefaults.AuthenticationScheme)时,我会收到一个我不想要的登录提示。

我的理解是,即使我想将它用于 Active Directory,我也不需要 Active Directory 或域来验证 Windows 用户。

环境:

依赖项:

启动:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<IISOptions>(iis =>
    {
        iis.AuthenticationDisplayName = "Windows";
        iis.AutomaticAuthentication = true;
    });

    services.AddAuthentication(IISDefaults.AuthenticationScheme);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
        {                
            UserName = context?.User?.Identity?.Name
        }));
    });       

启动设置.json:

{
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:51682/",
      "sslPort": 0      
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

网络配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore" />
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore forwardWindowsAuthToken="true" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

applicationhost.config:(IIS Express)

基于这篇文章:https ://docs.microsoft.com/en-us/iis/configuration/system.webServer/security/authentication/windowsAuthentication/

<authentication>
  <anonymousAuthentication enabled="true" userName="" />
  <basicAuthentication enabled="false" />
  <clientCertificateMappingAuthentication enabled="false" />
  <digestAuthentication enabled="false" />
  <iisClientCertificateMappingAuthentication enabled="false"></iisClientCertificateMappingAuthentication>
  <windowsAuthentication enabled="true">
    <providers>
      <add value="NTLM" />
    </providers>
  </windowsAuthentication>
</authentication>

标签: iisasp.net-coreactive-directory.net-corewindows-authentication

解决方案


几件事:

  1. 当您禁用匿名身份验证时,您会收到一个弹出窗口,因为浏览器可能不信任该站点。您需要打开 Internet 选项(从 Windows 控制面板)-> 安全选项卡 -> 单击“受信任的站点”-> 单击“站点”,然后将 URL 添加到您的站点。IE 和 Chrome 都使用这些设置来决定是否自动发送您的凭据。

  2. 当您同时启用匿名和 Windows 身份验证时,匿名优先,除非在您告诉它用户必须登录的地方。为此,请[Authorize]在控制器上或仅在单个操作上使用该属性。文档中有更多详细信息,位于Allow Anonymous Access标题下。


推荐阅读