MoonriseSoftwareCalifornia/AspNetCore.Identity.CosmosDb

NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.

toiyabe opened this issue · 1 comments

Describe the bug
When registering a new user, the following error occurs:

System.NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered. at Microsoft.AspNetCore.Identity.UserManager 1.GenerateUserTokenAsync(TUser user, String tokenProvider, String purpose) at Microsoft.AspNetCore.Identity.UserManager 1.GenerateEmailConfirmationTokenAsync(TUser user) at AspNetCore.Identity.CosmosDb.Example.Areas.Identity.Pages.Account.RegisterModel.OnPostAsync(String returnUrl) in C:\Users\eric\source\repos\CosmosSoftware\AspNetCore.Identity.CosmosDb\AspNetCore.Identity.CosmosDb.Example\Areas\Identity\Pages\Account\Register.cshtml.cs:line 119 at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Convert[T](Object taskAsObject) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.GenericTaskHandlerMethod.Execute(Object receiver, Object[] arguments) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync() at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterAsync() at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

To Reproduce
Steps to reproduce the behavior:

  1. Start with no database or containers created.
  2. Set the secrets so that the Cosmos DB provider can automatically create the database and containers.
  3. Run the website, and try to register a new user.
  4. See error.

Expected behavior
User account is created and a confirmation token is emailed to the new users

Additional context
It looks like the provider injection needs to add the default IUserTwoFactorTokenProvider.

This is a documentation mistake. One needs to add .AddDefaultTokenProviders() method in the startup file (Program.cs or Startup.cs) to create the default identity token providers like this:

//
// Add Cosmos Identity here
//
builder.Services.AddCosmosIdentity<CosmosIdentityDbContext<IdentityUser>, IdentityUser, IdentityRole>(
      options => options.SignIn.RequireConfirmedAccount = true
    )
    .AddDefaultTokenProviders();