C# OAuth OWIN: return custom response when invalid credentials provided to Token endpoint?
<p>I have a Web API project which implements authentication via <code>OAuthAuthorizationServerProvider</code>. I've subclassed the provider and have implemented methods as necessary to implement my own authentication system.</p>

<p>I also have figured out how to override the return value provided for unauthenticated requests (you have to subclass the <code>AuthorizeAttribute</code> class and then use your custom attribute instead of <code>Authorize</code> on endpoints you intend to secure). </p>

<p>I can also override the <code>TokenResponse</code> method in my OAuth auth server provider in order to alter the response containing the token.</p>

<p>Now what I'm trying to do is to override the response provided by the token endpoint when a user provides <em>incorrect</em> credentials to the token endpoint. Right now, I simply get this:</p>

<pre><code>{"error":"invalid_grant","error_description":"The user name or password is incorrect."}
</code></pre>

<p>I know where this text is coming from - in my <code>GrantResourceOwnerCredentials</code> method I do the following if a request is not authenticated:</p>

<pre><code>if (!isValidUser)
{         
    context.SetError("invalid_grant", "The user name or password is incorrect.");
    return;
}
</code></pre>

<p>Instead though, I want to be able to fully manipulate the <code>Response</code> object that is returned when the user provides incorrect credentials.</p>

<p>As an example, I might want to set the return to look like this:</p>

<pre><code>{"error":401,"timestamp":1234567890,"message":"Those credentials are wrong. Try again."}
</code></pre>

<p>Is there a way to override the response that the server provides upon <em>failed</em> authentication? </p>