I have a dotnet core 2.1 web application, which contains a react application. When the application is deployed to the Windows server using IIS windows authentication works properly. When debugging the application from Visual Studio 2017, using IIS, the authentication works properly. However, when running the application from VS Code authentication fails. This has proven exceptionally difficult and although there is documentation on both GitHub and the Microsoft documentation page, the implementation of that has not resolved the issue. Looking at the log file I can see that there is a System.Invalid operation exception and that no default or challenge scheme was found.
Error: System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Mvc.ChallengeResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
LaunchSettings.json:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:5000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationName": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:5000/"
}
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
if (HostingEnvironment.IsDevelopment())
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
services.Configure<IISOptions>(options => {
options.AutomaticAuthentication = true;
options.ForwardClientCertificate = true;
});
}
else
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
services.AddMvc();
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ReactApp/build";
});
}
Program.cs
public class Program
{
public static void Main(string[] args)
{
var logFactory = NLogBuilder.ConfigureNLog("nlog.config");
logFactory.KeepVariablesOnReload = true;
var logger = logFactory.GetCurrentClassLogger();
try
{
CreateWebHostBuilder(args).UseNLog().Build().Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception");
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
I also attempted using preprocessor directives, which Visual Studio 2017 responded too. However, this is a known issue with Visual Studio Code. I then attempted to use an extension method to determine if the request was coming from the localhost and then default the group based on the environment. This also did not work. I then attempted to differentiate between development environment as my local and use HttpSys with NTLM, based on this Stack Overflow question, but this also did not work. The above code has been reverted from the aforementioned attempts and the original code. Lastly, I have attempted both the IIS Express and the IISExpress executer extensions in Visual Studio Code still to no avail.