Пример #1
0
        public async Task ConfirmEmail(EmailConfirmRequest request)
        {
            if (request?.Code == null)
            {
                throw new AuthException("No code provided");
            }

            IdentityResult passwordValidationResult = await ValidatePassword(request.Password);

            if (!passwordValidationResult.Succeeded)
            {
                throw new AuthException(passwordValidationResult.Errors);
            }

            User user = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == request.UserId);

            if (user == null)
            {
                throw new AuthException("Incorrect user ID ");
            }

            bool isEmailTokenValid = await _userManager.VerifyUserTokenAsync(
                user,
                _userManager.Options.Tokens.EmailConfirmationTokenProvider,
                UserManager <User> .ConfirmEmailTokenPurpose,
                request.Code);

            if (!isEmailTokenValid)
            {
                throw new AuthException("Incorrect code", $"Email token is not valid for user {user}");
            }

            await _userManager.RemovePasswordAsync(user);

            IdentityResult passwordResult = await _userManager.AddPasswordAsync(user, request.Password);

            if (!passwordResult.Succeeded)
            {
                throw new AuthException(passwordResult.Errors);
            }

            user.EmailConfirmed = true;
            IdentityResult confirmEmailResult = await _userManager.UpdateAsync(user);

            if (!confirmEmailResult.Succeeded)
            {
                throw new AuthException(confirmEmailResult.Errors);
            }
        }
Пример #2
0
 public async Task ConfirmEmail(EmailConfirmRequest request)
 {
     await _authService.ConfirmEmail(request);
 }
Пример #3
0
        public async Task <IActionResult> EmailConfirm([FromBody] EmailConfirmRequest request)
        {
            if (request == null)
            {
                return(BadRequest(
                           new AuthFailedResponse
                {
                    Errors = new List <string>()
                    {
                        "Empty Request."
                    }
                }
                           ));
            }
            DragonflyUser user;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                user = await _userService.GetUserByIdAsync(HttpContext.GetUserId()).ConfigureAwait(false);
            }
            else if (!string.IsNullOrEmpty(request.UserName))
            {
                user = await _userService.GetUserByUserNameAsync(request.UserName).ConfigureAwait(false);
            }
            else if (!string.IsNullOrEmpty(request.Email))
            {
                user = await _userService.GetUserByEmailAsync(request.Email).ConfigureAwait(false);
            }
            else
            {
                return(BadRequest(
                           new AuthFailedResponse
                {
                    Errors = new List <string>()
                    {
                        "No user data included."
                    }
                }
                           ));
            }
            if (user == null)
            {
                return(BadRequest(
                           new AuthFailedResponse
                {
                    Errors = new List <string>()
                    {
                        "Could not find user."
                    }
                }
                           ));
            }
            var result = await _identityService.ConfirmEmailAsync(user, request.Token).ConfigureAwait(false);

            if (!result.Success)
            {
                return(BadRequest(
                           new AuthFailedResponse
                {
                    Errors = result.Errors
                }));
            }
            return(Ok(
                       new AuthenticationResult
            {
                Success = true
            }));
        }
        public async Task <IActionResult> EmailConfirm([FromBody] EmailConfirmRequest request)
        {
            if (request == null)
            {
                return(BadRequest(new BaseResponse <string>
                {
                    Errors = new List <string>()
                    {
                        "Empty Request."
                    },
                    Success = false,
                }));
            }
            AppUser user;

            if (HttpContext.User.Identity.IsAuthenticated)
            {
                var userId = _userManager.GetUserId(HttpContext.User);
                user = await _userManager.FindByIdAsync(userId);
            }
            else if (!string.IsNullOrEmpty(request.UserName))
            {
                user = await _userManager.FindByNameAsync(request.UserName);
            }
            else if (!string.IsNullOrEmpty(request.Email))
            {
                user = await _userManager.FindByEmailAsync(request.Email);
            }
            else
            {
                return(BadRequest(new BaseResponse <string>
                {
                    Errors = new List <string>()
                    {
                        "No user data included."
                    },
                    Success = false,
                }));
            }
            if (user == null)
            {
                return(BadRequest(new BaseResponse <string>
                {
                    Errors = new List <string>()
                    {
                        "Could not find user."
                    },
                    Success = false,
                }));
            }
            var result = await _accountService.ConfirmEmailAsync(user, request.Token);

            if (!result.Success)
            {
                return(BadRequest(new BaseResponse <string>
                {
                    Errors = result.Errors,
                    Success = false,
                }));
            }
            return(Ok(new BaseResponse <string>
            {
                Success = true
            }));
        }