title: Asp.Net Core Windows Authentication Not Working in IIS
body: <p>Asp.Net Core doesn't seem to recognize the user from the call <code>context?.User?.Identity?.Name</code> when windows authentication is enabled and running in IIS Express or IIS. </p>
  
  <p>Desired behavior: Enabling both Windows authentication and anonymous authentication in IIS and/or IIS Express, Asp.Net Core should automatically recognize the windows user.</p>
  
  <p>Actual behavior: When I enable both windows and anonymous authentication in IIS or IIS Express, the user name is null. When I disable anonymous authentication or call <code>HttpContext.ChallengeAsync(IISDefaults.AuthenticationScheme)</code>, I get a login prompt, which I don't want.</p>
  
  <p>My understanding is that, even though I want to use this for Active Directory, I don't need active directory or a domain to authenticate a windows user.</p>
  
  <p>Environment:</p>
  
  <ul>
  <li>Windows 8.1 (not on a domain)</li>
  <li>IIS 8.5 / Visual Studio 2017 w/ IIS Express</li>
  <li>Windows Authentication security feature installed</li>
  <li>Windows Authentication &amp; (with NTLM provider) &amp; Anonymous Authentication Enabled </li>
  <li>Logged in as local account user</li>
  </ul>
  
  <p>Dependencies:</p>
  
  <ul>
  <li>Microsoft.AspNetCore.All 2.0.8</li>
  </ul>
  
  <p>Startup:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.Configure&lt;IISOptions&gt;(iis =&gt;
      {
          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) =&gt;
      {
          await context.Response.WriteAsync(JsonConvert.SerializeObject(new
          {                
              UserName = context?.User?.Identity?.Name
          }));
      });       
  </code></pre>
  
  <p>launchSettings.json:</p>
  
  <pre><code>{
    "iisSettings": {
      "windowsAuthentication": true,
      "anonymousAuthentication": true,
      "iisExpress": {
        "applicationUrl": "http://localhost:51682/",
        "sslPort": 0      
      }
    },
    "profiles": {
      "IIS Express": {
        "commandName": "IISExpress",
        "launchBrowser": true,
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        }
      }
    }
  }
  </code></pre>
  
  <p>web.config:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;system.webServer&gt;
      &lt;handlers&gt;
        &lt;remove name="aspNetCore" /&gt;
        &lt;add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /&gt;
      &lt;/handlers&gt;
      &lt;aspNetCore forwardWindowsAuthToken="true" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" /&gt;
    &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>applicationhost.config: (IIS Express)</p>
  
  <p>Based on this article: <a href="https://docs.microsoft.com/en-us/iis/configuration/system.webServer/security/authentication/windowsAuthentication/" rel="noreferrer">https://docs.microsoft.com/en-us/iis/configuration/system.webServer/security/authentication/windowsAuthentication/</a></p>
  
  <pre><code>&lt;authentication&gt;
    &lt;anonymousAuthentication enabled="true" userName="" /&gt;
    &lt;basicAuthentication enabled="false" /&gt;
    &lt;clientCertificateMappingAuthentication enabled="false" /&gt;
    &lt;digestAuthentication enabled="false" /&gt;
    &lt;iisClientCertificateMappingAuthentication enabled="false"&gt;&lt;/iisClientCertificateMappingAuthentication&gt;
    &lt;windowsAuthentication enabled="true"&gt;
      &lt;providers&gt;
        &lt;add value="NTLM" /&gt;
      &lt;/providers&gt;
    &lt;/windowsAuthentication&gt;
  &lt;/authentication&gt;
  </code></pre>