コード例 #1
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByNameAsync(model.UserName);

                //if (user == null || !(await _userManager.IsPhoneNumberConfirmedAsync(user)))
                if (user == null)
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    //用户不存在(或者手机未验证),跳转注册页面
                    return(RedirectToAction(nameof(Register)));
                }

                // For more information on how to enable account confirmation and password reset please
                // visit https://go.microsoft.com/fwlink/?LinkID=532713
                //用于更新密码的token
                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                //var callbackUrl = Url.ResetPasswordCallbackLink(user.Id, code, Request.Scheme);
                //await _emailSender.SendEmailAsync(model.UserName, "Reset Password",
                //   $"Please reset your password by clicking here: <a href='{callbackUrl}'>link</a>");
                //用户存在,发送短信验证码
                string verificationCode = _messageSender.GetRandomNums();
                bool   result           = _messageSender.SendVerificationCode(user.UserName, verificationCode);
                if (result)
                {
                    _sendMessageLogService.InsertSendMessageLog(new SendMessageLog()
                    {
                        PhoneNumber = user.UserName,
                        SmsCode     = verificationCode,
                        //有效时间是30min内
                        InvalidTime = DateTime.Now.AddMinutes(30),
                        Sucess      = result,
                        IsChecked   = false
                    });
                    //后台跳转另一个页面带参数
                    return(RedirectToRoute(new
                    {
                        controller = "Account",
                        action = "ForgotPasswordConfirmation",
                        UserName = model.UserName,
                        Code = code
                    }));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }