DW24da
DW24da

Reputation: 147

OWIN OAuthAuthorizationServerProvider custom error response

For the current WebAPI, I use a custom implementation of the OAuthAuthorizationServerProvider.

I need to return a JSON response if the login has failed. This response is a simple ViewModel containing lockout time, etc. if any.

I Am however, unable to send the response. It either gets cut off at 25 characters (??) or sends it as an escaped C# string ("\"lockout": 2" etc.)

This is what i have tried so far:

context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Response.ContentType = "application/json; charset=utf-8";
context.Response.Write(JsonConvert.SerializeObject(authResult));

Upvotes: 1

Views: 1286

Answers (1)

DW24da
DW24da

Reputation: 147

Solved it by storing the content length in the Request context (temporarily).

context.Request.Set("Content-Length", json.Length.ToString());

Then obtain it in the Startup.cs

if (c.Request.Environment.ContainsKey("Content-Length"))
{
    c.Response.ContentLength = Convert.ToInt64(c.Request.Environment["Content-Length"]);
    c.Response.ContentType = "application/json; charset=utf-8";
    c.Response.StatusCode = (int)HttpStatusCode.Unauthorized;  
}  

Upvotes: 1

Related Questions