public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { return(Task.Factory.StartNew(() => { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var username = context.UserName; var password = context.Password; UsuarioService usuarioService = new UsuarioService(); Usuario usuario = usuarioService.ObterPorLogin(username); if (usuario != null && EncriptarSenha.CompararSenhas(context.Password, usuario.Senha)) { List <Claim> claims = gerarClaimsPorUsuario(usuario); ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, authenticationType: ""); context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { })); } else { context.SetError("invalid_grant", "Login inválido."); } })); }
public IHttpActionResult Get(string username, string password) { JwtResult objJwtResult = new JwtResult(); try { UsuarioService usuarioService = new UsuarioService(); Usuario usuario = usuarioService.ObterPorLogin(username); if (usuario != null && EncriptarSenha.CompararSenhas(password, usuario.Senha)) { string token = JwtManager.GenerateToken(usuario.IdUsuario.ToString()); objJwtResult.access_token = token; return(Ok(objJwtResult)); } else { return(Unauthorized()); } } catch (Exception erro) { return(BadRequest(erro.Message)); } }