The RequireHeaderAuthorization middleware currently throws a 403 Forbidden status code when authorization fails. However, in many cases, a 401 Unauthorized status code is more appropriate per HTTP standards, as it indicates missing or invalid authentication credentials. Additionally, there’s no way to customize the error message to provide more context to the client.
I had to create a custom middleware to replicate the functionality of RequireHeaderAuthorization but return a 401 status code and a custom error message. This workaround is not ideal, as it duplicates code.
Proposed Solution
Add configuration options to RequireHeaderAuthorization to allow developers to specify:
- A custom HTTP status code (e.g., 401 instead of 403).
- A custom error message to be returned in the response body.