コード例 #1
0
        public async Task <IActionResult> ForgotPassword([FromBody] EmailInputForm form)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _db.Users.FirstOrDefaultAsync(x => x.Email.Equals(form.Email, StringComparison.OrdinalIgnoreCase));

            if (user == null)
            {
                return(BadRequest(ControllerErrorCode.AccountNotFound));
            }

            var code = await _db.ForgotCodes.FirstOrDefaultAsync(x => x.User == user);

            if (code != null)
            {
                _db.ForgotCodes.Remove(code);
            }

            _generator.GenerateCode(1);

            var newCode = new ForgotPassCode
            {
                User       = user,
                Code       = _generator.Code,
                ExpireDate = _generator.ExpireDate
            };

            await _db.ForgotCodes.AddAsync(newCode);

            await _db.SaveChangesAsync();

            var msg = ComposeMessage(user.Email, "Forgot Password", $@"Your reset code: <b>{_generator.Code}</b>");

            await _sender.SendAsync(msg);

            return(Ok("Success"));
        }
コード例 #2
0
        public async Task <IActionResult> ResendCode([FromBody] EmailInputForm form)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ControllerErrorCode.WrongInputData));
            }

            var user = await _db.Users.FirstOrDefaultAsync(x => x.Email.Equals(form.Email, StringComparison.OrdinalIgnoreCase));

            if (user == null)
            {
                return(NotFound());
            }

            if (user.IsConfirmed)
            {
                return(BadRequest(ControllerErrorCode.AlreadyConfirmed));
            }

            await SendMailAndGenerateCode(user);

            return(Ok());
        }