How to fix 'InvalidOperationException: Scheme already exists: Bearer'
By FoxLearn 1/17/2025 2:45:51 AM 74
If you're registering the Bearer authentication scheme more than once in your Startup.cs
, you may encounter this error.
For example, you might be calling AddAuthentication()
multiple times like this:
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication() .AddJwtBearer("Bearer", options => { // Bearer JWT options }); // This could cause the error if it's repeated or redundant services.AddAuthentication() .AddJwtBearer("Bearer", options => { // Same Bearer JWT options }); }
If you have a custom bear, you should write with a different name 'Bear' as below.
public void ConfigureServices(IServiceCollection services) { // Code omitted for brevity services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Audience = "https://localhost:5000/"; options.Authority = "https://localhost:5000/identity/"; }) .AddJwtBearer("AzureAD", options => { options.Audience = "https://localhost:5000/"; options.Authority = "https://login.microsoftonline.com/eb971100-6f99-4bdc-8611-1bc8edd7f436/ "; }); }
If you are using custom authentication schemes, ensure that the name you are using for your scheme is unique and does not conflict with others, like Bearer
.
Make sure that "CustomBearer"
is not being registered again with the same name in any other part of the code.
Ensure that you are only adding the authentication scheme once.
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication("Bearer") .AddJwtBearer(options => { // Bearer JWT options }); }
If the authentication scheme is configured both in the ConfigureServices
and Configure
methods or if there are multiple places where authentication is added, this can also lead to conflicts.
Ensure that the authentication scheme is being added in the ConfigureServices
method and not being duplicated in the Configure
method.
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication("Bearer") .AddJwtBearer(options => { // JWT options }); } public void Configure(IApplicationBuilder app) { app.UseAuthentication(); // This should be done only once }
Please check if you have multiple Startup.cs
files and whether you are using any authentication schemes in those files.
Also, check the publish or deployment folder. Make sure to delete the App_Data
folder before deploying fresh or latest changes.
To fix the System.InvalidOperationException: Scheme already exists: bearer
error, make sure you're registering the authentication scheme only once, check for conflicts with other libraries, and ensure that your configuration isn't being duplicated.
- Content Negotiation in Web API
- How to fix System.InvalidOperationException: Scheme already exists: Identity.Application
- Add Thread ID to the Log File using Serilog
- Handling Exceptions in .NET Core API with Middleware
- InProcess Hosting in ASP.NET Core
- Limits on ThreadPool.SetMinThreads and SetMaxThreads
- Controlling DateTime Format in JSON Output with JsonSerializerOptions
- ‘s’ is an invalid start of a value. Path: $ in ASP.NET Core