'Best approach to customize OpenIddict 401 error response body

I'm using OpenIddict 3.1.1 to implement an IFTTT service with authentication (Authorization Code Flow) in ASP.NET Core 3.1. Unfortunately, IFTTT has specific requirements regarding the response body in case of a failed authentication (JSON array with error messages), which does not match the default case in OpenIddict (empty body).

I tried to add event handlers for ValidateAuthorizationRequest, ValidateTokenRequest or HandleAuthorizationRequest in Startup.cs/ConfigureServices to intercept the response and change the body, but none of them are ever called.

So my question is if this is even the right approach to do this, and if so, is there something I have missed that might explain why the event handlers don't get called?

options.AddEventHandler<ValidateAuthorizationRequestContext>(builder =>
    builder.UseInlineHandler(context =>
    {
        return default;
    }));
options.AddEventHandler<ValidateTokenRequestContext>(builder =>
    builder.UseInlineHandler(context =>
    {
        return default;
    }));
options.AddEventHandler<HandleAuthorizationRequestContext>(builder =>
    builder.UseInlineHandler(context =>
    {
        return default;
    }));


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source