"未找到IUserTokenProvider"调用UserManager.GeneratePasswordResetTokenAsync时出错

时间:2015-07-29 18:56:26

标签: c# asp.net-identity

有人可以帮助我使用以下代码吗?我在这条线上收到错误,并且不明白为什么:

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

2 个答案:

答案 0 :(得分:1)

GeneratePasswordResetTokenAsync方法需要在UserTokenProvider中设置UserManager

由于GenerateUserTokenAsync方法调用的GeneratePasswordResetTokenAsync中的空检查,您收到错误。

答案 1 :(得分:0)

斯科特布拉迪答案的补充。您应该手动创建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