title: Windows Authentication in .net Core 2.1 application
body: <p>I've got a .net Core 2.1 MVC application that hosts an Angular 6 application.  I am using Visual Studio 2017 15.7.3.  I'm trying to set up Windows Authentication but am having issues.  I've followed the documentation and my Program.cs and Startup.cs are below:</p>
  
  <p>Program.cs:</p>
  
  <pre><code>using Microsoft.AspNetCore.Server.HttpSys;
  using Microsoft.AspNetCore;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Logging;
  using NLog.Web;
  using System;
  
  namespace CoreIV_Admin_Core
  {
  public class Program
  {
      public static void Main(string[] args)
      {
          var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
  
          try
          {
              logger.Debug("init main");
              CreateWebHostBuilder(args).Build().Run();
          }
          catch (Exception ex)
          {
              //NLog: catch setup errors
              logger.Error(ex, "Stopped program because of exception");
              throw;
          }
          finally
          {
              // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
              NLog.LogManager.Shutdown();
          }
      }
  
      public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;()
              //.UseHttpSys(options =&gt;
              //{
              //    options.Authentication.Schemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
              //    options.Authentication.AllowAnonymous = false;
              //})
              .ConfigureLogging(logging =&gt;
              {
                  logging.ClearProviders();
                  logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information);
              })
              .UseNLog(); // NLog: setup NLog for Dependency injection
  }
  }
  </code></pre>
  
  <p>Startup.cs:</p>
  
  <pre><code>using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Server.HttpSys;
  using Microsoft.AspNetCore.SpaServices.Webpack;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.DependencyInjection.Extensions;
  
  namespace CoreIV_Admin_Core
  {
  public class Startup
  {
      public Startup(IConfiguration configuration)
      {
          Configuration = configuration;
      }
  
      public IConfiguration Configuration
      {
          get;
      }
  
      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
          {
              // This lambda determines whether user consent for non-essential cookies is needed for a given request.
              options.CheckConsentNeeded = context =&gt; true;
              options.MinimumSameSitePolicy = SameSiteMode.None;
          });
  
          services.TryAddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  
          services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);
  
          services.AddMvc()
                 .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      }
  
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
              app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
              {
                  HotModuleReplacement = true
              });
          }
          else
          {
              app.UseExceptionHandler("/Home/Error");
              app.UseHsts();
          }
  
          app.UseHttpsRedirection();
  
          app.UseStaticFiles();
  
          app.UseCookiePolicy();
  
          app.UseHttpContext();
  
          app.UseAuthentication();
  
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
  
              routes.MapSpaFallbackRoute(
                  name: "spa-fallback",
                  defaults: new
                  {
                      controller = "Home",
                      action = "Index"
                  });
          });
      }
   }
  }
  </code></pre>
  
  <p>If I uncomment the <code>.UseHttpSys</code> section in program.cs and press play in Visual Studio to debug, it almost immediately stops the debug session and the following error is in the Event log:</p>
  
  <blockquote>
    <p>"Application 'MACHINE/WEBROOT/APPHOST/myapp' with physical root
    'C:\Users\me\myapp' created process with commandline 'C:\Program Files
    (x86)\Microsoft Visual
    Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\Web
    Tools\ProjectSystem\VSIISExeLauncher.exe -argFile
    "C:\Users\me\AppData\Local\Temp\tmpFCA6.tmp"' but failed to listen on
    the given port '28353'".</p>
  </blockquote>
  
  <p>If I try with <code>.UseHttpSys</code> commented out, the debugging works but I am unable to be properly authenticated.</p>