コード例 #1
0
ファイル: ForgotPassword.cshtml.cs プロジェクト: xuebai5/emud
        public async Task <IActionResult> OnPostAsync([FromBody] SendResetEmailDto dto)
        {
            if (!ModelState.IsValid)
            {
                return(await Task.FromResult(new JsonResult(new
                {
                    Status = false,
                    ErrorMessage = ModelState.Where(e => e.Value.Errors.Count > 0).Select(e => e.Value.Errors.First().ErrorMessage).First()
                })));
            }

            var userId = _accountContext.UserId;

            var command = new SendResetEmailCommand(dto.Email);
            await _bus.SendCommand(command);

            if (_notifications.HasNotifications())
            {
                var errorMessage = string.Join(";", _notifications.GetNotifications().Select(x => x.Content));
                return(await Task.FromResult(new JsonResult(new
                {
                    status = false,
                    errorMessage
                })));
            }

            return(await Task.FromResult(new JsonResult(new
            {
                status = true
            })));
        }
コード例 #2
0
        public async Task <Unit> Handle(SendResetEmailCommand command, CancellationToken cancellationToken)
        {
            var email = command.Email;

            var user = await _userDomainService.Get(p => p.Email == email && p.HasVerifiedEmail);

            if (user == null)
            {
                await _bus.RaiseEvent(new DomainNotification("输入信息有误,请确认邮箱是否无误"));

                return(Unit.Value);
            }

            int expiryMin = 30;

            string key = string.Format(RedisKey.ResetPassword, user.Id);// $"resetpassword_{user.Id}";

            if (await _redisDb.KeyTimeToLive(key) > 0)
            {
                await _bus.RaiseEvent(new DomainNotification($"找回密码操作需要间隔24小时"));

                return(Unit.Value);
            }


            string code = GenerateRandom(4);

            try
            {
                string url = $"{_appConfig.Site.Url}/user/resetpassword?email={email}&code={code}";
                await _mail.Send(new MailModel
                {
                    Content = $"<p>您好,您正在使用找回密码功能,请在验证码输入框中输入此次验证码: {code},验证码有效期{expiryMin}分钟。</p><p>您也可以<a href='{url}'>点击这里</a>或复制以下链接到浏览器打开</p><p>{url}</p>",
                    Address = email,
                    Subject = $"【{_appConfig.Site.Name}】找回密码"
                });
            }
            catch (Exception ex)
            {
                _logger.LogError($"邮件发送失败  {ex}");
                await _bus.RaiseEvent(new DomainNotification($"邮件发送失败,请稍后重试"));

                return(Unit.Value);
            }

            await _redisDb.StringSet(key, code, DateTime.Now.AddMinutes(expiryMin));

            if (await Commit())
            {
                await _bus.RaiseEvent(new ResetPasswordEvent(user)).ConfigureAwait(false);
            }


            return(Unit.Value);
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync(SendResetEmailDto dto)
        {
            var userId = _accountContext.UserId;

            var command = new SendResetEmailCommand(dto.Email);
            await _bus.SendCommand(command);

            if (_notifications.HasNotifications())
            {
                var errorMessage = string.Join(";", _notifications.GetNotifications().Select(x => x.Content));
                return(await Task.FromResult(new JsonResult(new
                {
                    status = false,
                    errorMessage
                })));
            }

            return(await Task.FromResult(new JsonResult(new
            {
                status = true
            })));
        }