Esempio n. 1
0
        public async Task <AuthResult <Token> > RestorePassword(RestorePasswordDTO restorePasswordDto)
        {
            if (restorePasswordDto == null ||
                string.IsNullOrEmpty(restorePasswordDto.Email) ||
                string.IsNullOrEmpty(restorePasswordDto.Token) ||
                string.IsNullOrEmpty(restorePasswordDto.NewPassword) ||
                string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) ||
                string.IsNullOrEmpty(restorePasswordDto.ConfirmPassword) ||
                restorePasswordDto.ConfirmPassword != restorePasswordDto.NewPassword
                )
            {
                return(AuthResult <Token> .UnvalidatedResult);
            }

            var user = await userManager.FindByEmailAsync(restorePasswordDto.Email);

            if (user != null && user.Id > 0)
            {
                var result = await userManager.ResetPasswordAsync(user, restorePasswordDto.Token, restorePasswordDto.NewPassword);

                if (result.Succeeded)
                {
                    var token = jwtManager.GenerateToken(user);
                    return(AuthResult <Token> .TokenResult(token));
                }
            }

            return(AuthResult <Token> .UnvalidatedResult);
        }
Esempio n. 2
0
        public async Task <AuthResult <Token> > RefreshToken(RefreshTokenDTO refreshTokenDto)
        {
            var refreshToken = refreshTokenDto?.Token?.Refresh_token;

            if (string.IsNullOrEmpty(refreshToken))
            {
                return(AuthResult <Token> .UnvalidatedResult);
            }

            try
            {
                var principal = jwtManager.GetPrincipal(refreshToken, isAccessToken: false);
                var userId    = principal.GetUserId();
                var user      = await userManager.FindByIdAsync(userId.ToString());

                if (user != null && user.Id > 0)
                {
                    var token = jwtManager.GenerateToken(user);
                    return(AuthResult <Token> .TokenResult(token));
                }
            }
            catch (Exception)
            {
                return(AuthResult <Token> .UnauthorizedResult);
            }

            return(AuthResult <Token> .UnauthorizedResult);
        }
Esempio n. 3
0
        public async Task <AuthResult <Token> > Login(LoginDTO loginDto)
        {
            if (loginDto == null || string.IsNullOrEmpty(loginDto.Email) || string.IsNullOrEmpty(loginDto.Password))
            {
                return(AuthResult <Token> .UnvalidatedResult);
            }

            var user = await userManager.FindByEmailAsync(loginDto.Email);

            if (user != null && user.Id > 0)
            {
                if (await userManager.CheckPasswordAsync(user, loginDto.Password))
                {
                    var token = jwtManager.GenerateToken(user);
                    return(AuthResult <Token> .TokenResult(token));
                }
            }

            return(AuthResult <Token> .UnauthorizedResult);
        }
Esempio n. 4
0
        public async Task <AuthResult <string> > RequestPassword(RequestPasswordDTO requestPasswordDto)
        {
            if (requestPasswordDto == null ||
                string.IsNullOrEmpty(requestPasswordDto.Email))
            {
                return(AuthResult <string> .UnvalidatedResult);
            }

            var user = await userManager.FindByEmailAsync(requestPasswordDto.Email);

            if (user != null && user.Id > 0)
            {
                var passwordResetToken = await userManager.GeneratePasswordResetTokenAsync(user);

                var token = AuthResult <string> .TokenResult(passwordResetToken);

                SendRequestPasswordEmail(token.Data, user);
                return(token);
            }

            return(AuthResult <string> .UnvalidatedResult);
        }
Esempio n. 5
0
        public async Task <AuthResult <Token> > SignUp(SignUpDTO signUpDto)
        {
            if (signUpDto == null ||
                string.IsNullOrEmpty(signUpDto.Email) ||
                string.IsNullOrEmpty(signUpDto.Password) ||
                string.IsNullOrEmpty(signUpDto.ConfirmPassword) ||
                string.IsNullOrEmpty(signUpDto.FirstName) ||
                string.IsNullOrEmpty(signUpDto.LastName) ||
                signUpDto.Password != signUpDto.ConfirmPassword
                )
            {
                return(AuthResult <Token> .UnvalidatedResult);
            }

            var newUser = new TUser {
                FirstName = signUpDto.FirstName,
                LastName  = signUpDto.LastName,
                Login     = signUpDto.Email,
                Email     = signUpDto.Email
            };

            var result = await userManager.CreateAsync(newUser, signUpDto.Password);

            if (result.Succeeded)
            {
                if (newUser.Id > 0)
                {
                    await userManager.AddToRoleAsync(newUser, "User");

                    var token = jwtManager.GenerateToken(newUser);
                    return(AuthResult <Token> .TokenResult(token));
                }
            }

            return(AuthResult <Token> .UnauthorizedResult);
        }