简体   繁体   中英

Asp.Net Core No DefaultChallengeScheme found

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM