Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 283
Cybercop Avatar asked Mar 25 '14 09:03

Cybercop


3 Answers

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.

like image 189
meziantou Avatar answered Oct 18 '22 19:10

meziantou


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.

like image 35
pisker Avatar answered Oct 18 '22 19:10

pisker


In addition to the accepted answer, I'd like to add that this approach will not work in Azure Web-Sites, you'd get CryptographicException instead of a token.

To get it fixed for Azure, implement your own IDataProtector: see this answer

Slightly more detail in blog-post

like image 22
trailmax Avatar answered Oct 18 '22 18:10

trailmax