Exemplo n.º 1
0
        public async Task <GetLoginResponseDto> Login(PostLoginDto postLoginDto)
        {
            var user = await _unityOfWork.UserRepository.GetAsync(user => user.Email == postLoginDto.Email);

            var isInvalidPassword = user == null || !_hashService.Compare(user.Password, postLoginDto.Password);

            if (isInvalidPassword)
            {
                throw new NotFoundException("Usuário e/ou senha incorreta");
            }

            return(new GetLoginResponseDto
            {
                AccessToken = _tokenService.Generate(user)
            });
        }
Exemplo n.º 2
0
        private async Task <AuthResult> AuthActionAsync(LoginDto login)
        {
            var users = (await _userRepository.GetAllAsync(new UserByEmailSpec(login.Email))).ToArray();

            if (users.Length != 1)
            {
                return(AuthResult.Fail());
            }

            var targetUser = users[0];

            if (!_hashService.Compare(targetUser, login.Password))
            {
                return(AuthResult.Fail());
            }

            var claims = _claimProvider.GetClaims(targetUser);

            return(AuthResult.Success(claims, _tokenFactory.Create(claims)));
        }
        public async Task Consume(ConsumeContext <ToolBox.Contracts.Auth.AuthenticateUser> context)
        {
            _logger.LogInformation("AuthenticateUserTestConsumer Called");

            var userIn = await _dbContext.Users
                         .Include(u => u.UserRoles)
                         .ThenInclude(u => u.Role)
                         .SingleOrDefaultAsync(u => u.UserName == context.Message.UsernameOrEmail ||
                                               u.Email == context.Message.UsernameOrEmail);

            if (userIn == null)
            {
                await context.RespondAsync <NotFound>(new
                {
                    Message = $"User: {context.Message.UsernameOrEmail} was not found"
                });
            }

            var result = _hashService.Compare(context.Message.Password, userIn.PasswordHash, userIn.PasswordSalt);

            switch (result)
            {
            case PasswordVerificationResult.Failed:
                await context.RespondAsync <NotFound>(new
                {
                    Message = $"Wrong password or username"
                });

                break;

            case PasswordVerificationResult.Success:
                await context.RespondAsync <Token>(new
                {
                    Token = _authService.GenerateJsonWebToken(userIn)
                });

                break;
            }
        }