C# – Asp.Net Core No DefaultChallengeScheme found

asp.net-coreauthenticationc

I have a custom AuthorizationHandler in my Asp.Net Core API project which authorizes requests based on Tokens been passed in the Headers.

The Handler works fine as in it invoked on runtime however am getting the following error

InvalidOperationException: No authenticationScheme was specified, and there was no
DefaultChallengeScheme found.

I have registered my settings in Startup.cs as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    services.AddSingleton<IAuthorizationHandler, TokenAuthorizationHandler>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseAuthentication();
    app.UseMvc();
}

Any suggestions?

Best Answer

The default properties of the project is enable Anonymous Authentication and disable Windows Authentication .

If you want to choose IIS default authentication as your authenticationScheme , you need to modify the project's properties to enable Windows Authentication and disable Anonymous Authentication :

  • Right-click the project in Solution Explorer and select Properties.

  • Select the Debug tab.

  • Clear the check box for Enable Anonymous Authentication.

  • Select the check box for Enable Windows Authentication.

  • Save and close the property page.

Reference : https://docs.microsoft.com/en-us/aspnet/core/security/authentication/windowsauth?view=aspnetcore-2.2&tabs=visual-studio#iisiis-express

Or you could add others authenticationSchemes (cookie-based authentication , JWT bearer authentication etc) , refer to Authorize with a specific scheme in ASP.NET Core for more details .

Related Topic