System.InvalidOperationException: Scheme already exists: Identity.Application
means Identity's cookie schemes were registered twice. In the lab the cause was
AddIdentity<IdentityUser, IdentityRole>() called twice; with one call left,
the app listed its four Identity schemes and started.
Search the project for every AddIdentity call, including those inside your own
extension methods, and keep one. Other pairs that register the same cookies, such as
AddDefaultIdentity or AddIdentityCookies next to AddIdentity,
were not run here.
The error
Server console, at startup, on the error stream; the top of the stack trace:
Unhandled exception. System.InvalidOperationException: Scheme already exists: Identity.Application
at Microsoft.AspNetCore.Authentication.AuthenticationOptions.AddScheme(String name, Action`1 configureBuilder)
at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
at Microsoft.Extensions.Options.UnnamedOptionsManager`1.get_Value()
at Microsoft.AspNetCore.Authentication.AuthenticationSchemeProvider..ctor(IOptions`1 options, IDictionary`2 schemes)
...
Why it happens
The broken Program.cs had the registration twice, the second one standing in for
a helper method that also "sets up Identity":
...
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDb>();
// Added later, in an extension method that also "sets up Identity".
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDb>();
...
Both calls compiled and builder.Build() passed. The exception came when the
authentication options were first built: the stack runs from the
AuthenticationSchemeProvider constructor into AddScheme, which refused
a second scheme with the same name. The lab triggered that by resolving
IAuthenticationSchemeProvider in a startup scope. An app without such a line would
meet the same options at the first code that reads the authentication schemes, which this lab
did not test, so where your copy of the message appears depends on that code.
The message names only the first scheme that collided, Identity.Application.
Removing the duplicate call as a whole was the fix that ran, and the scheme list then showed four schemes.
The fix
...
builder.Services.AddDbContext<AppDb>(o => o.UseSqlite("Data Source=lab.db"));
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppDb>();
...
With one call the same scope printed the schemes Identity registered:
Scheme: Identity.Application
Scheme: Identity.External
Scheme: Identity.TwoFactorRememberMe
Scheme: Identity.TwoFactorUserId
...
Printing the scheme list like this is a quick check after you remove a call. The lab used two
AddIdentity calls because they need no extra package; the
AddDefaultIdentity plus AddIdentity combination, which needs the
Identity UI package, was not run here.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, Identity registered twice with
AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDb>(),
and a scope after builder.Build() that resolves
IAuthenticationSchemeProvider and prints every scheme name; IdnLab, port
15620, AppDb and the lab.db name in its connection string are the test's. The project was built with dotnet build and
started with dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620, once with
both calls and once with one (it printed
Hosting environment: Production).
Frequently asked
- What does Scheme already exists: Identity.Application mean?
- The Identity cookie schemes were registered twice. In the lab AddIdentity<IdentityUser, IdentityRole>() was called twice; keeping one call fixed it.
- Where do I look for the duplicate Identity registration?
- Search for every AddIdentity call across Program.cs and your own extension methods. The lab's second call stood in for a helper method that also set up Identity; other duplicate pairs were not run.
- Which schemes does AddIdentity register?
- In the lab one AddIdentity call registered Identity.Application, Identity.External, Identity.TwoFactorRememberMe and Identity.TwoFactorUserId.
More decoded errors in the Fixes category, and the other startup message about authentication setup in No sign-in authentication handlers are registered.