public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var identity = new ClaimsIdentity(context.Options.AuthenticationType); context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); using (AsyncScopedLifestyle.BeginScope(_container)) { try { IUsuarioAppService service = _container.GetInstance <IUsuarioAppService>(); AutenticaUsuarioViewModel usuarioRequest = new AutenticaUsuarioViewModel(); usuarioRequest.Email = context.UserName; usuarioRequest.Senha = context.Password; var usuario = service.Autentica(usuarioRequest); if (usuario == null) { context.SetError("invalid_grant", "E-mail ou senha incorreta."); return; } identity.AddClaim(new Claim("Usuario", JsonConvert.SerializeObject(usuario))); var principal = new GenericPrincipal(identity, new string[] { }); Thread.CurrentPrincipal = principal; context.Validated(identity); } catch (Exception ex) { context.SetError("invalid_grant", ex.Message); return; } } }