Support customizing `RemoteAuthenticationHandler.ShouldHandleRequestAsync` via event
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
I'd like to skip a `RemoteAuthenticationHandler` early (before it does any request processing) using custom logic.
### Describe the solution you'd like
A new event on `RemoteAuthenticationEvents` ("OnCallback"? "OnRequestHandling"? another better name...?) where I can specify a `Func>` (or similar) that'd be called by `ShouldHandleRequestAsync` to customize the logic for deciding when the auth handler should handle the request.
### Additional context
We have a multi-tenant app where each tenant is tied to a unique hostname and a different IdP registration, hence each tenant has its own OIDC scheme.
Other than the hostname, each tenant's URLs should look basically the same, so the different OIDC handlers all have the same callback path.
For the most part, we have an "entrypoint" / "proxy" auth scheme that directs the current request to the appropriate auth scheme based on its host. However, the callback logic (i.e. `HandleRequestAsync`) cannot be proxied, because all auth handlers (that implement `IAuthenticationRequestHandler`) get to handle the request directly:
https://github.com/dotnet/aspnetcore/blob/d32513d151d18f3fb32465b97a118303de145115/src/Security/Authentication/Core/src/AuthenticationMiddleware.cs#L48-L57
We currently solve this problem by using the [`SkipUnrecognizedRequests`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.openidconnect.openidconnectoptions.skipunrecognizedrequests) in OIDC options.
However
- this is inefficient because the wrong handlers are doing unnecessary processing of the request before being skipped
- this is not ideal in terms of tenant separation - ideally the auth handler for a tenant wouldn't even look inside the request body of another tenant's request
- the same approach doesn't work for `OAuthHandler` because `OAuthEvents` doesn't have a `SkipUnrecognizedRequests`
We can alternatively solve this problem by inheriting handlers like `OpenIdConnectHandler` and `OAuthHandler`, and override `ShouldHandleRequestAsync()`, but then we lose convenience methods like `AddOpenIdConnect` and `AddOAuth`, and the code structure is harder to understand (compared to using events)
Contributor guide
Assessment
This issue has not been assessed yet.