1

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));
Cœur
  • 37,241
  • 25
  • 195
  • 267
DW24da
  • 147
  • 1
  • 3
  • 13
  • Did you set the `Response.ContentLength` property? – Federico Dipuma May 25 '16 at 13:19
  • Yes tried that, seems however to set it back to 25 – DW24da May 25 '16 at 13:33
  • 1
    I believe the OAuth provider is simply overriding your response (and in that case is appending to the same stream, but overriding the ContentLength header). Look at this answer, it may help you find a solution (the issue is the same): http://stackoverflow.com/a/36755639/3670737 – Federico Dipuma May 25 '16 at 13:41

1 Answers1

1

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;  
}  
DW24da
  • 147
  • 1
  • 3
  • 13