[release/10.0-preview7] Avoid cookie login redirects for known API endpoints #62894
+1,170
−71
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Backport of #62816 to release/10.0-preview7
/cc @halter73
Avoid cookie login redirects for known API endpoints
By default, the CookieAuthenticationHandler redirects to the login page for unauthenticated requests. This does not make sense as a default behavior for API endpoints like those defined in an [ApiController] and most minimal APIs.
This PR adds IApiEndpointMetadata automatically to [ApiController]-defined endpoints and many minimal APIs where we can definitely determine that it's not returning HTML or other endpoint intended for direct browser navigation.
Description
This adds IApiEndpointMetadata as new public API. It doesn't add any attributes or extension methods, but we do add it automatically for endpoints where RDF/RDG read a JSON request body or write a JSON response body. ApiController endpoints, SignalR endpoints and endpoints with TypedResults return-types that we know to be API-oriented also get this metadata automatically.
API Proposal: #62883
Fixes: #9039
Even though IApiEndpointMetadata has not yet been approved by API Review, most users will not need to directly interact with this API to benefit from it. The vast majority of developers will not need to make any changes to their code even if the API shape changes.
Customer Impact
#9039 is one of the most thumbs-upped issues in the aspnetcore repo. Without this change, customers need to apply workarounds like the following to prevent the CookieAuthenticationHandler from redirecting to the login page for unauthenticated API requests which is a hassle when you don't already know about the workaround given that we could provide a better experience by default which we do with this PR.
Regression?
Risk
We only add the new IApiEndpointMetadata to endpoints we are confident are not intended for direct browser navigation. Furthermore, this new metadata is only referenced by the default implementations of CookieAuthenticationEvents.OnRedirectToLogin and OnRedirectToAccessDenied which are overridable.
Verification
Packaging changes reviewed?