public IActionResult RequestToken([FromBody] AutenticacaoModel autenticacao) { try { Cliente cli = null; ErroModel erro = null; using (var db = new ZupContext()) { string senha = UtilsHelper.CriptografaSenha(autenticacao.Senha); cli = db.Cliente.Where(w => w.Email.Equals(autenticacao.Email) && w.Senha.Equals(senha)).FirstOrDefault(); } if (cli != null) { var claims = new[] { new Claim(ClaimTypes.Email, autenticacao.Email), new Claim(ClaimTypes.Role, "Admin"), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["SecurityKey"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: "teste", audience: "teste", claims: claims, expires: DateTime.Now.AddMinutes(30), signingCredentials: creds); return(Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token) })); } erro = new ErroModel() { Mensagem = "Usuário ou senha inválidos." }; return(NotFound(erro)); } catch (Exception er) { return(StatusCode((int)HttpStatusCode.InternalServerError, new ErroModel() { Mensagem = er.Message, StackTrace = er.StackTrace })); } }
public IActionResult Post(Cliente cliente) { try { using (var db = new ZupContext()) { cliente.Senha = UtilsHelper.CriptografaSenha(cliente.Senha); db.Cliente.Add(cliente); db.SaveChanges(); } return(Ok()); } catch (Exception er) { return(StatusCode((int)HttpStatusCode.InternalServerError, new ErroModel() { Mensagem = er.Message, StackTrace = er.StackTrace })); } }