public HttpResponseMessage PostCliente(Clientes cliente) { TokenRepositorio tokenRepo = new TokenRepositorio(); try { if (cliente == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Cliente não informado.")); } repositorio.Insere(cliente); var token = tokenRepo.GeraToken(cliente); return(Request.CreateResponse(HttpStatusCode.Created, new { cliente, token })); } catch (EnderecoNaoSalvoException ex) { return(Request.CreateResponse(HttpStatusCode.Created, cliente, ex.Message)); } catch (GerarTokenException ex) { return(Request.CreateResponse(HttpStatusCode.Created, cliente, ex.Message)); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public HttpResponseMessage AutenticarComToken(string login, string senha) { TokenRepositorio tokenRepo = new TokenRepositorio(); if (login != "" && senha.Trim().Length > 0) { SqlConnection _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); SqlCommand _comandoSQL = new SqlCommand("select * from app_cliente where email = @email and senha = @senha", _conn); _comandoSQL.Parameters.AddWithValue("@email", login); _comandoSQL.Parameters.AddWithValue("@senha", senha); try { _conn.Open(); var dr = _comandoSQL.ExecuteReader(); if (!dr.HasRows) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Email ou senha inválidos.")); } } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } finally { _conn.Close(); } } List <Clientes> clientes = (List <Clientes>)repositorio.GetAll(); Clientes cliente = clientes.Find(c => c.Email.ToLower() == login.ToLower()); cliente.Enderecos = null; try { var token = tokenRepo.GeraToken(cliente); return(Request.CreateResponse(HttpStatusCode.Created, new { cliente, token })); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.Created, ex.Message)); } }