Return http status code with sub status from AuthorizeAttribute HandleUnauthorizedRequest

2.4k views Asked by At

How can I return a http status code with sub status like 403.7 from HandleUnauthorizedRequest of inheritor of AuthorizeAttribute? I know how to return a http status code without sub status:

protected override void HandleUnauthorizedRequest(AuthorizationContext ctx)
{
    ctx.Result = new HttpStatusCodeResult(403);
}

But I cannot find sub status code in HttpStatusCodeResult constructor.

[UPDATE] I also try this code, however response start line looks like "HTTP/1.1 403 Forbidden" (not HTTP/1.1 403.7 Forbidden):

 protected override void HandleUnauthorizedRequest(AuthorizationContext ctx)
 {
     ctx.HttpContext.Response.StatusCode = 403;
     ctx.HttpContext.Response.SubStatusCode = 7;
     ctx.HttpContext.Response.End();
 }
1

There are 1 answers

0
DavidG On BEST ANSWER

While it is possible to set the substatus, that value is never passed to the client and is only useful for things like logging.

Instead you need to get the data to the client in another way. You can either use the response body, or in this case, I'd probably set a custom HTTP header which you will be able to read in your client:

ctx.HttpContext.Response.AddHeader("Status", "SomethingWentWrong");