public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { // 请不要显示该用户不存在或者未经确认 return View("ForgotPasswordConfirmation"); } // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771 // 发送包含此链接的电子邮件 // 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, "重置密码", "请通过单击 <a href=\"" + callbackUrl + "\">此处</a>来重置你的密码"); // return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); }
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { //首先核对用户的短信验证码是否合法 using (SystemDbContext vCode = new SystemDbContext()) { var CurrentUserCode = vCode.VerifyCodes.Find(model.PhoneNumber); DateTime CurTime = System.DateTime.Now; if (CurTime > CurrentUserCode.OverTime) //用户短信验证码超时 { ModelState.AddModelError("", "抱歉,您的验证码已经过期!"); return View(model); } else if (!CurrentUserCode.Code.Equals(model.Code)) { ModelState.AddModelError("", "抱歉,您的验证码输入错误!"); return View(model); } } ApplicationUser user = UserManager.FindByName(model.PhoneNumber); var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var result = UserManager.ResetPassword(user.Id, code, model.Password); if (result.Succeeded) { return RedirectToAction("ResetPasswordConfirmation"); } // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771 // 发送包含此链接的电子邮件 // 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, "重置密码", "请通过单击 <a href=\"" + callbackUrl + "\">此处</a>来重置你的密码"); // return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return View(model); }