Scheme already exists: Bearer

3.1k views Asked by At

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();

Why is there that exception?

1

There are 1 answers

1
Yiyi You On

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)]