我需要使用Asp.Net Identity 2.1.0实现忘记密码功能,但UserManager.GeneratePasswordResetTokenAsync挂起且从未返回,我甚至尝试过UserManager.GneratePasswordresetToken(user.Id),但没有用。
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByNameAsync(model.UserId);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
{
ModelState.AddModelError("", "The user either does not exist or is not confirmed.");
return View();
}
//Send an email with this link
string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href="" + callbackUrl + "">here</a>");
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
// If we got this far, something failed, redisplay form
return View(model);
}
我不知道缺了什么,非常感谢你的帮助。
好吧,我解决了这个问题。我创建了另一个空项目,发现UserManager.GeneratePasswordResetTokenAsync(user.Id)在那里运行良好。我从另一个项目中删除了Microsoft.Identity.Core和其他相关的包,然后重新添加,现在它就像一个魅力。