How to fix 'InvalidOperationException: Scheme already exists: Bearer'

By FoxLearn 1/17/2025 2:45:51 AM   74
The System.InvalidOperationException: Scheme already exists: bearer error typically occurs when you're working with authentication in a .NET application, and you've registered the Bearer authentication scheme multiple times, which causes a conflict.

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.