public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { var clientIP = Request.UserHostAddress.ToString(); if (!IPCount.CheckIsAble(clientIP)) { ModelState.AddModelError("UserName", "已经超过尝试次数"); } if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.UserName); if (user == null) { // 请不要显示该用户不存在或者未经确认 ModelState.AddModelError("", "用户不存在!"); } else { var useremail = user.Email.Substring(0, 2) + "***" + user.Email.Substring(user.Email.IndexOf('@')); //身份验证 var preVerify4ResetPasswordModel = new PreVerify4ResetPasswordModel { UserName = user.UserName, Email = useremail }; return(View("Verify4ResetPassword", preVerify4ResetPasswordModel));//跳转到身份认证 } //var result4VerifyCode = // await PhoneVerifyCodeProvider.CodeVerify(user.PhoneNumber, model.VerifyCode, TimeSpan.Zero); //if (!result4VerifyCode.Success)//验证码验证 //{ // ModelState.AddModelError("VerifyCode", "验证码错误!"); //} //var aa = await UserManager.PasswordValidator.ValidateAsync(model.Password); //if (!aa.Succeeded) //{ // foreach (var error in aa.Errors) // { // ModelState.AddModelError("", error); // } //} //if (!ModelState.IsValid) //{ // return View(model); //} //await UserManager.RemovePasswordAsync(user.Id); //await UserManager.AddPasswordAsync(user.Id, model.Password); //return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return(View(model)); }
public async Task <ActionResult> Verify4ResetPassword(PreVerify4ResetPasswordModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.UserName); if (user == null) { return(RedirectToAction("ForgotPassword")); } //验证身份 var success = false; #region : 验证码验证 var result4VerifyCode = await EmailVerifyCodeProvider.CodeVerify(user.Email, model.VerifyCode, TimeSpan.FromMinutes(30)); if (!result4VerifyCode.Success) { foreach (var err in result4VerifyCode.ErroMessage) { ModelState.AddModelError(err.Key, err.Value); } } else { success = true; } #endregion if (success) { //生成重置密码的Code string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); return(View("ResetPassword", new ResetPasswordModel { Code = code, UserId = user.Id })); //跳转到密码重置} } } return(View(model)); }