1

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.

  • It is because the extensions you tried are not from Microsoft. Report such bugs to the developers or hack the source code on your own. VSCode is just a code editor, so you should not assume it would be as powerful as Visual Studio. – Lex Li Sep 21 '18 at 02:30
  • I understand that the extensions are not from Microsoft. I also understand that VSCode is an editor. However, there are numerous documentation sources that state use of kestrel with IIS as a reverse proxy will work. I have been unable to get it to work. The reason that I pointed out the extensions is so that was not provided as the sole fix to this concern. This should not need an extension to run as a reverse proxy though. – theenterpriseprogrammer Sep 21 '18 at 07:10
  • Did u find any solution for this? Trying to figure out how to debug my asp.net core app in iis express with win auth via vscode – CodingSlayer Mar 02 '20 at 22:40

0 Answers0