Esempio n. 1
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. 2
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);
        }