IUserTokenProvider未找到!调用UserManager.GeneratePasswordResetTok

本文关键字:调用 UserManager GeneratePasswordResetTok IUserTokenProvider | 更新日期: 2023-09-27 18:05:20

有人能帮我下面的代码吗?我在这行得到一个错误,不明白为什么:

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

完整代码:

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);

IUserTokenProvider未找到!调用UserManager.GeneratePasswordResetTok

GeneratePasswordResetTokenAsync方法需要在您的UserManager中设置UserTokenProvider

您正在接收错误,因为您的GeneratePasswordResetTokenAsync方法调用的GenerateUserTokenAsync中存在空检查。

对Scott Brady的回答的补充。您应该手动创建UserTokenProvider:

// 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