public async Task <IActionResult> ResetPassword([FromBody] ResetPasswordCommand command) { try { if (command == null) { return(await Response(null, new List <Notification> { new Notification("Reset", "Reset Inválido") })); } var contract = new AuthenticateUserCommandContract(command); if (contract.Contract.Invalid) { return(await Response(command, contract.Contract.Notifications)); } var result = _authenticateAppService.ResetarSenha(command); return(await Response(result, null)); } catch (Exception ex) { return(await Response(null, new List <Notification> { new Notification("ResetSenha", ex.Message) })); } }
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)); }
public async Task <IActionResult> Post([FromForm] AuthenticateUserCommand command) { try { if (command == null) { return(await Response(null, new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } var contract = new AuthenticateUserCommandContract(command); if (contract.Contract.Invalid) { return(await Response(command, contract.Contract.Notifications)); } var identity = await GetClaims(command); if (identity == null) { return(await Response(null, 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, await _tokenOptions.JtiGenerator()), new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_tokenOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64), identity.FindFirst("Nome"), identity.FindFirst("IdPessoaPai"), identity.FindFirst("IdPessoa") }; //Adiciona um ou mais perfis foreach (var item in identity.FindAll("Perfil")) { claims.Add(item); } 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 } }; var json = JsonConvert.SerializeObject(response); return(new OkObjectResult(json)); } catch (Exception ex) { return(await Response(null, new List <Notification> { new Notification("User", "Usuário ou senha inválidos") })); } }