Use IHttpClientFactory in RemoteAuthenticationOptions
See original GitHub issueIs your feature request related to a problem? Please describe.
ASP.NET Core 2.1 introduced IHttpClientFactory to solve the problems with creating and disposing HttpClient with every call, and the problems with creating a long-lived HttpClient.
However, long-lived HttpClient is still created within the framework. Specifically, in RemoteAuthenticationOptions.Backchannel and RemoteAuthenticationOptions.BackchannelHttpHandler, these have effectively application lifetime because instances of AuthenticationOptions are cached in the singleton IOptionsMonitorCache.
Describe the solution you’d like
Replace RemoteAuthenticationOptions.Backchannel and RemoteAuthenticationOptions.BackchannelHttpHandler with RemoteAuthenticationOptions.BackchannelFactory, so that whenever an HttpClient is needed, it is created from the BackchannelFactory and disposed.
This would also require a new HttpDocumentRetriever constructor that takes an IHttpClientFactory, because JwtBearerPostConfigureOptions, WsFederationPostConfigureOptions and OpenIdConnectPostConfigureOptions initializes a long-lived ConfigurationManager.
Describe alternatives you’ve considered
-
Do not store AuthenticationOptions in IOptionsMonitorCache and make it scoped per request. However, it means that JwtBearer, OpenIdConnect and WsFederation (or 3rd party Saml2 protocol handler) would require fetching metadata on every request, which would slow down every request.
-
Roll my own ConfigurationManager and HttpDocumentRetriever, which would bypass any kind of niceities that
.AddJwtBearer,.AddOpenIdConnectextension methods provide.
Additional context
The Grand Auth Redesign of 2017 in Core 2.0 predates IHttpClientFactory in Core 2.1.
In Core 2.0, the new SocketsHttpHandler does not solve DNS problem
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9 (6 by maintainers)

Top Related StackOverflow Question
.NET Core 2.1 introduced
SocketsHttpHandleras the default handler, which is designed to be used in a long-lived singleton-like way and can be made to work. In the documentation for alternatives to IHttpClientFactory, it says that setting a finiteSocketsHttpHandler.PooledConnectionLifetimesolves the stale DNS problem of long-lived handlers while keeping the performance benefits of pooled resource management. The default value forSocketsHttpHandler.PooledConnectionLifetimeisTimeout.InfiniteTimeSpan, so default construction ofHttpClientorSocketsHttpHandlerwon’t have the behavior and it has to be manually set.This should allow us to use an application-lifetime (non-disposed)
HttpClientfor theBackchannel(orBackchannelHttpHandler) as a workaround.If you still want to use
IHttpClientFactory(it’s got really good delegating handler building overloads) and can work within the fact that theBackchannelwill be application-lifetime, you could get fancy with using DI services to configure options.Sorry, I’m not confident enough on this topic to make an assertion.