i have to show a custom(json) response body when the authorization fails in my API. By default i have this message : Unauthorized. But i would like to return a json containing a customized code error, a message and some other details. Here is an example of what i did.
o.Events = new JwtBearerEvents
{
OnChallenge = async (context) =>
{
if (!context.Request.Headers.ContainsKey("Authorization"))
{
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "The authorization parameter is not given or the token passed is empty";
}
},
OnAuthenticationFailed = async (context) =>
{
await context.HttpContext.Response.WriteAsync("ramses");
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "The token is invalid or has expired";
},
};
i tried with WriteAsync method of HttpContext.Response but the message i defined does not appear, only the reason phrase appears correctly.
here is the image of the result in my API.
i specify this code in situated in startup.cs on jwtBearer configuration
