Exemple #1
0
        public IActionResult Login([FromBody] LoginForm loginForm)
        {
            if (!(loginForm is null) && ModelState.IsValid)
            {
                loginForm.Passwd = _cryptoService.Decrypter(Convert.FromBase64String(loginForm.Passwd));

                try
                {
                    User user = _authRepository.Login(loginForm);
                    user = _authRepository.Authenticate(user);

                    if (user.Id == 0)
                    {
                        return(NoContent());
                        //return BadRequest(new { message = "Wrong email or password" }); ////*** AuthRequester
                    }
                    else
                    {
                        return(Ok(user));
                    }
                }
                catch (Exception ex)
                {
                    return(BadRequest());
                }
            }
            return((!(loginForm is null)) ? BadRequest(ModelState) : BadRequest("There is no Data !"));
        }
        public ActionResult <ResponseModelDTO> Authenticate([FromBody] UserCreateDTO user)
        {
            var userModel = _mapper.Map <UserModel>(user);
            var response  = _repository.Authenticate(userModel);

            _repository.SaveChanges();
            return(Ok(response));
        }
        public User Authenticate(string username, string password)
        {
            User usuario = _authenticateRepository.Authenticate(username, password);

            return(usuario);
        }
        public ICommandResult Handle(AuthenticateUserCommand command)
        {
            if (command == null)
            {
                return(new CommandResult(false, new List <Notification> {
                    new Notification("User", "Usuário ou senha inválidos")
                }));
            }

            var contract = new AuthenticateUserCommandContract(command);

            if (contract.Contract.Invalid)
            {
                return(new CommandResult(false, contract.Contract.Notifications));
            }

            //Busca o Usuário no banco de dados
            Usuario _usuario = _repository.Authenticate(command.CPF, command.Senha);

            if (_usuario == null)
            {
                return(new CommandResult(false, new List <Notification> {
                    new Notification("User", "Usuário ou senha inválidos")
                }));
            }

            var claims = new List <Claim>()
            {
                new Claim(JwtRegisteredClaimNames.UniqueName, command.CPF),
                new Claim(JwtRegisteredClaimNames.NameId, command.CPF),
                new Claim(JwtRegisteredClaimNames.Email, command.CPF),
                new Claim(JwtRegisteredClaimNames.Sub, command.CPF),
                new Claim(JwtRegisteredClaimNames.Jti, _tokenOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_tokenOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                new Claim("Nome", _usuario.Nome),
                new Claim("IdPessoa", _usuario.IdPessoa.ToString()),
                new Claim("IdPessoaPai", _usuario.IdPessoaPai.ToString()),
            };

            var jwt = new JwtSecurityToken(
                issuer: _tokenOptions.Issuer,
                audience: _tokenOptions.Audience,
                claims: claims.AsEnumerable(),
                notBefore: _tokenOptions.NotBefore,
                expires: _tokenOptions.Expiration,
                signingCredentials: _tokenOptions.SigningCredentials);

            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            var response = new
            {
                token   = encodedJwt,
                expires = (int)_tokenOptions.ValidFor.TotalSeconds,
                user    = new
                {
                    id          = _usuario.Id,
                    name        = _usuario.Nome,
                    idPessoaPai = _usuario.IdPessoaPai,
                    idPessoa    = _usuario.IdPessoa,
                    firstName   = _usuario.FirstName,
                    imagem      = _usuario.Imagem
                }
            };

            return(new CommandResult(true, response));
        }
Exemple #5
0
        public Usuario Authenticate(string emailcpf, string senha)
        {
            Usuario usuario = _authenticateRepository.Authenticate(emailcpf, senha);

            return(usuario);
        }