kimeshan chetty
kimeshan chetty

Reputation: 11

"IUserTokenProvider not found" error when calling UserManager.GeneratePasswordResetTokenAsync

Can someone help me with the following code? I'm getting an error on this line and don't understand why:

string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);

Full code:

var user = await UserManager.FindByEmailAsync(model.Email);//Find user by email entered
if (user == null)
{
    return View("ForgotPasswordConfirmation");
}
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Login", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

Upvotes: 0

Views: 384

Answers (2)

danpop
danpop

Reputation: 997

A complementary to Scott Brady's answer. You should create the UserTokenProvider manually:

// db is of type DbContext or IdentityDbContext
var userManager = new UserManager(new UserStore(db));
var dataProtectionProvider = new DpapiDataProtectionProvider("Test");
userManager.UserTokenProvider = new DataProtectorTokenProvider<User, Guid>(dataProtectionProvider.Create("ASP.NET Identity"));
var user = await UserManager.FindByEmailAsync(model.Email);//Find user by email entered
//rest of the code

Upvotes: 0

Scott Brady
Scott Brady

Reputation: 5598

The GeneratePasswordResetTokenAsync method requires a UserTokenProvider to be set in your UserManager.

You are receiving the error because of a null check in the GenerateUserTokenAsync called by your GeneratePasswordResetTokenAsync method.

Upvotes: 1

Related Questions