public Status Autenticar(Usuario user) { if (user == null) throw new HttpResponseException(new HttpResponseMessage() { StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("Por favor informe as credencias de acesso.") }); var usuario = new AplicacaoUsuario().UsuarioValido(user); if (usuario != null) { Token token = new Token(usuario.Id, Request.GetClientIP()); return new Status { Successeded = true, Token = token.Criptografar(), Message = "Login realizado com sucesso." }; } else { throw new HttpResponseException(new HttpResponseMessage() { StatusCode = HttpStatusCode.Unauthorized, Content = new StringContent("Usuario ou senha invalidos.") }); } }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { const string tokenName = "X-Token"; if (request.Headers.Contains(tokenName)) { var tokenCriptografado = request.Headers.GetValues(tokenName).First(); try { var token = Token.Descriptografar(tokenCriptografado); var usuarioIdValido = new AplicacaoUsuario().UsuarioIdValido(token.UsuarioId); var ip = request.GetClientIP(); var ipValido = token.Ip.Equals(ip); if (!usuarioIdValido || !ipValido) { var reply = request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Identidade apresentada não é válida!"); return Task.FromResult(reply); } } catch (Exception ex) { var reply = request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Token usado não é válido!"); return Task.FromResult(reply); } } else { var reply = request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Esta requisição exige um token de autorização!"); return Task.FromResult(reply); } //return base.SendAsync(request, cancellationToken); var request2 = new RequestEnvelope(request); if (!request2.IsCorsPreflight) return base.SendAsync(request, cancellationToken); request.Headers.Add("Access-Control-Allow-Origin", "*"); const string supportedMethods = "POST, PUT, GET, DELETE, X-Token"; request.Headers.Add("Access-Control-Allow-Methods", supportedMethods); return base.SendAsync(request, cancellationToken); }