public ActionResult RetrievePassword(RetrievePassword model) { ViewBag.HasError = false; if (ModelState.IsValid) { if (UnitOfWork <UserManager>().RetrievePassword(model.UserName, model.PasswordAnswer)) { ViewBag.PasswordLength = UnitOfWork <UserManager>().MinPasswordLength; return(RedirectToAction("RetrievePasswordSuccess")); } ViewBag.HasError = true; ModelState.AddModelError("", ControllerResources.AccountController_RetrievePassword_The_current_password_is_incorrect_or_the_new_password_is_invalid_); } // If we got this far, something failed, redisplay form ViewBag.PasswordLength = UnitOfWork <UserManager>().MinPasswordLength; return(View(model)); }
/// <summary> /// 找回密码。发送邮件 /// </summary> /// <param name="param"></param> /// <returns></returns> public async Task <Result> RetrievePasswordAsync(RetrievePassword param, string emailTemplate) { /* * 1,查找用户id,找到关联问题,验证答案 * 2,发送验证码、记录验证码以及有效期 * 3,另起修改密码的接口,验证码失效机制 * **/ var user = await masterDbSet.Where(a => a.AccountName == param.AccountName).Include(a => a.UserSecurityQuestions) .FirstOrDefaultAsync(a => a.UserSecurityQuestions.Any(q => q.QuestionID == param.QuestionID && q.Answer == param.Answer)); if (user == null) { throw new CustomException("AnsowerError", "找回密码失败"); } if (user.IsDisabled) { throw new CustomException("CurrentUserIsDisabled", "账户被禁用"); } //todo:参照原系统中GetEmailVerificationCode_V_1_2 方法。发送邮件 if (user.Email.IsNullOrWhiteSpace()) { throw new CustomException("EmailIsEmpty", "邮箱为空"); } var regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); if (!regex.IsMatch(user.Email)) { throw new CustomException("EmailError", "邮箱信息错误"); } var vCode = new VerificationCode(user.AccountName, VerificationCodeType.ChangePasswordByCode); var redisKey = ConfigService.GetVerificationCodeRedisKey(user.AccountName, VerificationCodeType.ChangePasswordByCode, param.SysCode); var validTime = Convert.ToInt32(BaseCore.AppSetting["VerificationCodeValidTime"]); redisCache.Set(redisKey, vCode, TimeSpan.FromMinutes(validTime)); var subject = ""; var sendContent = emailTemplate.Replace("{UserName}", user.UserName) .Replace("{EmailCode}", vCode.Code) .Replace("{ValidDateTime}", validTime.ToString()); if (param.Url.IsNullOrWhiteSpace()) { sendContent = sendContent.Replace("{Action}", ""); } else { if (param.Lang == Language.zh_CN) { subject = BaseCore.AppSetting["RetrievePasswordEmail:EmailSubject_zh"]; sendContent = sendContent.Replace("{Action}", BaseCore.AppSetting["RetrievePasswordEmail:ChangePassWordByCodeEmail_zh"]); } else { subject = BaseCore.AppSetting["RetrievePasswordEmail:EmailSubject_en"]; sendContent = sendContent.Replace("{Action}", BaseCore.AppSetting["RetrievePasswordEmail:ChangePassWordByCodeEmail_en"]); } sendContent = sendContent.Replace("{Url}", "<a href='" + param.Url + "' target='_blank'>" + param.Url + "</a>"); } TEGEMailHelper.SendTE2UNoReplyEmail(user.Email, subject, sendContent); //todo:log记录 return(new SuccessResult()); }
public async Task <ActionResult <Result> > RetrievePasswordAsync(RetrievePassword param) { var tem = param.Lang == Language.zh_CN ? Resource.EmailVerificationCode_zh_CN : Resource.EmailVerificationCode_en_US; return(await _userService.RetrievePasswordAsync(param, tem)); }