No IUserTokenProvider is registered

I recently updated Asp.Net Identity Core of my application form 1.0 to 2.0.

There are new features which I wanted to try like GenerateEmailConfirmationToken, etc.

I'm using this project as a reference.

When the user tries to register, I get error during the execution of Post method of Register

private readonly UserManager<ApplicationUser> _userManager;     

public ActionResult Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        var ifUserEXists = _userManager.FindByName(model.EmailId);
        if (ifUserEXists == null) return View(model);
        var confirmationToken = _userRepository.CreateConfirmationToken();//this is how i'm generating token currently.                
        var result = _userRepository.CreateUser(model,confirmationToken);
        var user = _userManager.FindByName(model.EmailId);
        if (result)
        {
            var code = _userManager.GenerateEmailConfirmationToken(user.Id);//error here
            _userRepository.SendEmailConfirmation(model.EmailId, model.FirstName, confirmationToken);
            //Information("An email is sent to your email address. Please follow the link to activate your account.");
            return View("~/Views/Account/Thank_You_For_Registering.cshtml");
        }     
    }
    
    //Error("Sorry! email address already exists. Please try again with different email id.");
    ModelState.AddModelError(string.Empty, Resource.AccountController_Register_Sorry__User_already_exists__please_try_again_);
    return View(model);
}

In the line

 var code = _userManager.GenerateEmailConfirmationToken(user.Id);

I get error saying:

No IUserTokenProvider is registered.

For now, I just wanted to see what kind of code it generates. Is there some change I need to make to my ApplicationUser class that inherits from IdentityUser class?

Or is there something I need to change to get those function work?


You have to specify a UserTokenProvider to generate a token.

using Microsoft.Owin.Security.DataProtection;
using Microsoft.AspNet.Identity.Owin;
// ...

var provider = new DpapiDataProtectionProvider("SampleAppName");

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

userManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(
    provider.Create("SampleTokenName"));

You should also read this article: Adding Two Factor Authentication to an Application Using ASP.NET Identity.


In ASP.NET Core it's nowadays possible to configure a default service in the Startup.cs like this:

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddDefaultTokenProviders();

There is no need to call the DpapiDataProtectionProvideror anything like that. The DefaultTokenProviders() will take care of the call to GenerateEmailConfirmationToken from the UserManager.