System.NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.
was thrown by GenerateEmailConfirmationTokenAsync because Identity had been
registered with AddIdentityCore and no token providers. Add
.AddDefaultTokenProviders() to the Identity builder, and with
AddIdentityCore also builder.Services.AddDataProtection();.
The second line matters: in the lab, adding the token providers alone swapped this message for
a different one about IDataProtectionProvider. Both lines together produced a token.
The error
Console output of a startup seed that creates a user and asks for its confirmation token (the first line on standard output, the exception on the error stream):
Create user: Succeeded
Unhandled exception. System.NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.
...
Why it happens
The registration was the minimal one:
...
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>();
...
Creating the user worked, so the store was fine. The token call asks for a provider named
Default, and this registration had none, which is what the message says. The same
provider name is used by the email confirmation call in this lab; other token calls were not
tested here.
Adding .AddDefaultTokenProviders() on its own then failed one step later, with:
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.DataProtection.IDataProtectionProvider' while attempting to activate 'Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1[Microsoft.AspNetCore.Identity.IdentityUser]'.
...
The default provider is DataProtectorTokenProvider, and nothing in this
AddIdentityCore project had registered data protection for it.
Note the exception types. The first message is a NotSupportedException thrown
when the token is requested, after the user already exists in the database. The second is an
InvalidOperationException from dependency injection: its frames show it while
UserManager was being resolved (GetRequiredService, the
UserManager constructor, activating DataProtectorTokenProvider), before
CreateAsync. In that run nothing reached standard output at all, not even
Create user. In these two runs, the first type came with no providers and the second
with providers but no data protection.
The fix
...
builder.Services.AddDbContext<AppDb>(o => o.UseSqlite("Data Source=lab.db"));
builder.Services.AddDataProtection();
builder.Services.AddIdentityCore<IdentityUser>()
.AddEntityFrameworkStores<AppDb>()
.AddDefaultTokenProviders();
...
With both lines the seed printed Create user: Succeeded and
Token length: 264. The lab registered no cookies, antiforgery or Razor components;
whether one of those removes the need for the data-protection line was not tested 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, a seed in a scope after
builder.Build() that creates one fictional user and calls
GenerateEmailConfirmationTokenAsync. IdnLab, AppDb, lab.db,
port 15620, the user and its password are the test's; a real seed reads a password from configuration or user-secrets,
never from source. Three runs, each built with dotnet build and started with
dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620: no providers (in a new
copy of the base project), providers only, and providers with data protection (the fixed run
printed Hosting environment: Production); the later runs moved the previous file aside first.
Frequently asked
- How do I fix No IUserTwoFactorTokenProvider named Default is registered?
- Add .AddDefaultTokenProviders() to the Identity registration. With AddIdentityCore, also call builder.Services.AddDataProtection(), or the provider cannot be created.
- Why does AddDefaultTokenProviders fail with IDataProtectionProvider?
- The default token provider needs data protection. In the lab AddIdentityCore did not register it, so builder.Services.AddDataProtection() was needed as well.
- Which Identity calls need a token provider?
- In the lab GenerateEmailConfirmationTokenAsync asked for the provider named Default. Other token methods were not tested here.
A token that is produced but later refused gives 'Invalid token.' instead. More decoded errors in the Fixes category.