Closed unionthugface closed 2 years ago
I'm closing this out because I just stopped using this method and used instead app.UseStatusCodePages
:
app.UseStatusCodePages(async (StatusCodeContext statusCodeContext) =>
{
var context = statusCodeContext.HttpContext;
if (context.Response.StatusCode == 401)
{
await context.Response.Body.WriteAsync(JsonSerializer.SerializeToUtf8Bytes(new ErrorListResponseModel().AddError("401", "UNAUTHORIZED")));
}
});
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.
I have a business case where I need to add a custom JSON response to 401 unauthorized requests. I am trying to hook into the
OnAuthenticationFailed
event -- which maybe isn't the right place?I created a SO question for this: https://stackoverflow.com/questions/71562924/net-core-3-1-oauth-2-token-introspection-with-custom-json-response-status-co
I'll copy/paste some of that here. Here's my code:
This produces the error:
The logging callsite of this error is
Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReportApplicationError
.Whether I add or remove
context.NoResult()
-- no difference.I've tried using a synchronous delegate function and returning
Task.CompletedTask
instead -- no difference.If I don't explicitly set the
context.Response.StatusCode
, I get a200
status code in the response, AND the error still throws!The remarks in the library suggest that
. . . but I don't see any suggestions on how to suppress these exceptions.
Expected behavior: Write a custom JSON object to 401 unauthorized response without triggering an exception.