ASP.NET Core 2.2 - Use HTTP.sys only in Development

1.6k views Asked by At

I have an application in ASP.NET Core 2.2 which requires Windows Authentication. To have a full development environment, we use HTTP.sys because it allows us to have full NTLM support with the watcher:

dotnet watch run

The problem occurrs, when we deploy into IIS 10.0

Startup file is configured as following:

services.AddAuthentication(HttpSysDefaults.AuthenticationScheme);
services.Configure<IISOptions>(options =>
{
    options.AutomaticAuthentication = true;
});

Program is configured as following:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost
            .CreateDefaultBuilder(args)
            .UseIISIntegration()
            .UseHttpSys(options =>
            {
                options.Authentication.Schemes = AuthenticationSchemes.NTLM;
                options.Authentication.AllowAnonymous = false;
            })
            .UseStartup<Startup>()
            .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(hostingContext.Configuration)
                .Enrich.FromLogContext()
                .WriteTo.Console());
}

On local development everything works as expected. When we deploy, we have the following errors:

If we set the web.config hostingModel="InProcess"

2019-10-16 17:03:14.320 +02:00 [Fatal] Application startup exception
System.InvalidOperationException: Application is running inside IIS process but is not configured to use IIS server.
   at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

If we remove it, so Out Process:

2019-10-16 17:10:38.151 +02:00 [Fatal] Application startup exception
System.InvalidOperationException: Scheme already exists: Windows
   at Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider.AddScheme(AuthenticationScheme scheme)
   at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware..ctor(RequestDelegate next, ILoggerFactory loggerFactory, IOptions`1 options, String pairingToken, Boolean isWebsocketsSupported, IAuthenticationSchemeProvider authentication, IApplicationLifetime applicationLifetime)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
0

There are 0 answers