'Scheme already exists: Bearer

I'm implementing login using Microsoft provider also implement the default login using jwt

when I run the project I got this error (Scheme already exists: Bearer)

when i comment this part, project run successfully

 //.AddJwtBearer(x =>
           // {
           //     x.SaveToken = true;
           //     x.TokenValidationParameters = tokenValidationParameters;
           // })

here is my code


            var jwtSettings = new JWTSettings();
            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)),
                ValidateIssuer = false,
                ValidateAudience = false,
                RequireExpirationTime = false,
                ValidateLifetime = true
            };
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.SaveToken = true;
                x.TokenValidationParameters = tokenValidationParameters;
            })
            .AddMicrosoftIdentityWebApi(configuration, "AzureAd")
                  .EnableTokenAcquisitionToCallDownstreamApi()
                      .AddMicrosoftGraph(configuration.GetSection("DownstreamApi"))
                         .AddInMemoryTokenCaches();


Solution 1:[1]

Scheme already exists: Bearer

It means you have more than one schemes with the same name.

You can try to set the parameter authenticationScheme into JwtBearerExtensions.AddJwtBearer Method.Here is the official doc.

And if you want to select the scheme,you can refer to the doc,and try to use:

[Authorize(AuthenticationSchemes = xxx)]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yiyi You