ASP Core 2.0 app.UseJwtBearerAuthentication fails

Viewed 8043

I have developed a web application using dot net core 1.1. It was working fine till I've updated to asp core 2.0. Then when trying to use the application, it reports this error:

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

The authentication config of JwtBearerAuthentication, in the Configure method of Startup.cs was previously like bellow:

app.UseJwtBearerAuthentication(new JwtBearerOptions
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                TokenValidationParameters = tokenValidationParameters
            });

After facing this error I have updated that to this one:

app.UseJwtBearerAuthentication(new JwtBearerOptions
        {
            AutomaticAuthenticate = true,
            AutomaticChallenge = true,
            TokenValidationParameters = tokenValidationParameters,
            AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme
        });

But still the error message is alive. Am I looking wrong place for the error or I have have to add additional configurations for the authentication system to work?

2 Answers

You can use this code instead of your own code to solve your problem:

    services.AddAuthentication(options =>
    {
     options.DefaultAuthenticateScheme = 
     JwtBearerDefaults.AuthenticationScheme;
     options.DefaultChallengeScheme = 
     JwtBearerDefaults.AuthenticationScheme;
     options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
    })
Related