Explicitly calling `context.Fail(....)` in authorization policy requirement handler results in empty `policyAuthorizationResult.AuthorizationFailure.FailedRequirements`
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
Calling context.Fail(....) in our Authorization policy requirement handler causes policyAuthorizationResult.AuthorizationFailure.FailedRequirements to be empty when our Authorization fails.
Expected Behavior
policyAuthorizationResult.AuthorizationFailure.FailedRequirements should not be empty when Authorization fails regardless of whether we call context.Fail(....) in our policy requirement handler or not.
Steps To Reproduce
-
Clone this repo, build and run.
-
Initiate the request below using
cURL, notice we get a401 Unauthorizedresponse.
curl --location 'https://localhost:7244/authbugdemo/implicit-fail' \
--header 'Content-Type: application/json' \
--header 'X-API-Key: BadAPIKey' \
--data '{
"name": "Chucky",
"description": "Child'\''s Play is an American slasher media franchise created by Don Mancini."
}'
- Initiate the request below using
cURL, notice we get a403 Forbiddenresponse.
curl --location 'https://localhost:7244/authbugdemo/explicit-fail' \
--header 'Content-Type: application/json' \
--header 'X-API-Key: BadAPIKey' \
--data '{
"name": "Chucky",
"description": "Child'\''s Play is an American slasher media franchise created by Don Mancini."
}'
-
the difference between the two endpoints are their
Authorizationpolicy requirement handlers.The
implicit-failendpoint policy requirement handler doesn't callcontext.Failif authorization fails. however, theexplicit-failendpoint policy requirement handler callscontext.Fail("....")when authorization fails. -
I have an
IAuthorizationMiddlewareResultHandlerimplementation that checks if the failed requirement is of a particular type usingpolicyAuthorizationResult.AuthorizationFailure.FailedRequirementsand modifies the response status code appropriately.
unfortunately, policyAuthorizationResult.AuthorizationFailure.FailedRequirements is always empty when we use the explicit-fail policy requirement handler that calls context.Fail("....").
I don't see this behaviour documented anywhere so I assume it's a bug.
If it is an expected behaviour (this would be strange :confused:), then how do we propagate the failure reason from the various policy requirement handlers to my IAuthorizationMiddlewareResultHandler without calling context.Fail("....")?
Exceptions (if any)
No response
.NET Version
8.0.200
Anything else?
No response