How to fix System.InvalidOperationException: Scheme already exists: Identity.Application

By FoxLearn 2/17/2025 6:53:10 AM   1.72K
The error message System.InvalidOperationException: Scheme already exists: Identity.Application typically occurs when you're trying to add Identity to your ASP.NET Core project, but the scheme "Identity.Application" is already registered in the authentication options.

If you create an ApplicationUser class that extends the IdentityUser class, then add a FullName property or another property.

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }
}

Opening your Startup class, then modify your class as shown below.

// fix already exists: identity.application scheme
services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

Make sure you haven't accidentally configured Identity multiple times in your Startup.cs file. Check for duplicate calls to services.AddIdentity or any other related methods.

If you find duplicate configurations related to Identity in your Startup.cs, remove the redundant ones.

The error message System.InvalidOperationException: 'scheme already exists: bearer' occurs when your application tries to register the same authentication scheme (bearer in this case) more than once. This typically happens when you're configuring authentication for JWT (JSON Web Token) bearer tokens, and you accidentally register the same scheme multiple times in your application's startup.

Double-check your configuration in ConfigureServices to ensure you're registering the bearer authentication scheme only once.

// fix scheme already exists: bearer
public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Bearer";
    })
    .AddJwtBearer("Bearer", options =>
    {
        options.Authority = "https://your-auth-provider";
        options.Audience = "your-api";
    });

    // Other services...
}

If you're adding the same scheme twice, it will trigger the InvalidOperationException. So make sure you don't have duplicate calls to AddJwtBearer for the same scheme.

You may have two different calls to AddAuthentication that both try to register the Bearer scheme. Ensure that you’re not registering it twice in different parts of the code.

// First call
services.AddAuthentication()
    .AddJwtBearer("Bearer", options => { /* config */ });

// Second call (problematic)
services.AddAuthentication()
    .AddJwtBearer("Bearer", options => { /* config */ });

If you want to ensure that the scheme is not registered multiple times, you can add conditional logic to avoid it.

if (!services.Any(x => x.ServiceType == typeof(IAuthenticationSchemeProvider)))
{
    services.AddAuthentication()
        .AddJwtBearer("Bearer", options => { /* config */ });
}

Remove any redundant calls to AddAuthentication() or AddJwtBearer().

The error message System.InvalidOperationException: 'Scheme already exists: Cookies' typically occurs when there is an attempt to register the same authentication scheme or service more than once, specifically for the Cookies scheme.

You might be registering the cookie authentication scheme multiple times in your Startup.cs or Program.cs file.

You need to check if you have multiple calls to services.AddAuthentication() or services.AddCookieAuthentication(). Make sure that the cookie authentication is registered only once.

// fix scheme already exists: cookies
public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                // Configure cookie options here
            });
}

Another reason might be a conflict in how the middleware is configured. If the AddAuthentication() is being configured multiple times, it might be causing this error.

Double-check the configuration of AddAuthentication and make sure you aren't configuring multiple schemes.

public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = "/Account/Login";
                options.AccessDeniedPath = "/Account/AccessDenied";
            });

    // other services
}

If you are trying to add a custom authentication scheme, ensure that the scheme name you provide is unique and doesn't conflict with built-in schemes like Cookies.

Ensure you’re not overriding Cookies if you are registering custom schemes.

services.AddAuthentication("MyCustomScheme")
        .AddCookie("MyCustomScheme", options =>
        {
            // Configure custom options here
        });

Verify your AddAuthentication() configuration and ensure that the cookie scheme is only registered once.