Repository navigation
Description
This issue is for a: (mark with an x)
- [ ] bug report -> please search issues before submitting
- [x ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)
Why?
Architectural guidance recommand to Use IHttpClientFactory to implement resilient HTTP requests.
ASP.NET Core IHttpClientFactory manages the pooling and lifetime of underlying HttpClientMessageHandler instances, which avoids port exhaustion and common DNS (Domain Name System) problems that occur when manually managing HttpClient lifetimes.
It also allows for configurable logging experience (via ILogger) for all requests sent through clients created by the factory.
Today, when instanciating a IConfidentialClientApplication in
microsoft-identity-web/src/Microsoft.Identity.Web/TokenAcquisition.cs
Lines 348 to 366 in 7293921
| if (_microsoftIdentityOptions.IsB2C) | |
| { | |
| authority = $"{ _applicationOptions.Instance}tfp/{_microsoftIdentityOptions.Domain}/{_microsoftIdentityOptions.DefaultUserFlow}"; | |
| app = ConfidentialClientApplicationBuilder | |
| .CreateWithApplicationOptions(_applicationOptions) | |
| .WithRedirectUri(currentUri) | |
| .WithB2CAuthority(authority) | |
| .Build(); | |
| } | |
| else | |
| { | |
| authority = $"{ _applicationOptions.Instance}{_applicationOptions.TenantId}/"; | |
| app = ConfidentialClientApplicationBuilder | |
| .CreateWithApplicationOptions(_applicationOptions) | |
| .WithRedirectUri(currentUri) | |
| .WithAuthority(authority) | |
| .Build(); | |
| } |
we leave it to MSAL.NET to manage the HttpClient
Possible design
For the moment, we could have the following design
- Add a new parameter
httpClientFactoryof typeIHttpClientFactoryin the constructor ofTokenAcquisition:so that an HttpClientFactor can be injected by dependency injection. Also add a corresponding property tomicrosoft-identity-web/src/Microsoft.Identity.Web/TokenAcquisition.cs
Lines 46 to 51 in 7293921
public TokenAcquisition( IMsalTokenCacheProvider tokenCacheProvider, IHttpContextAccessor httpContextAccessor, IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions, IOptions<ConfidentialClientApplicationOptions> applicationOptions, ILogger<TokenAcquisition> logger) TokenAcquisition - Add a new internal class (for instance
MsalAspNetCoreHttpClientFactory) implementing IMsalHttpClientFactory and having a constructor with an ASP.NET CoreIHttpClientFactory, and whichCreateClient()method would just delegate toIHttpClientFactory - When building MSAL.NET confidential client application, use the
.WithHttpClientFactory(httpClientFactory)modifyer and instanciate aMsalAspNetCoreHttpClientFactoryfrom thehttpClientFactoryproperty - In
AddWebAppCallsWebApiandAddProtectedWebApiCallsWebApi, add a service:services.AddHttpClient();
Possible future improvement
I don't think we need to use Named clients ?
Copied from Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2#267
Activity
jmprieur commentedon Feb 25, 2020
jennyf19 : this one would be useful. We might want to discuss with henrikm (@henrik-me) and Bogdan Gavril (@bgavrilMS) first if that's what we want to do (knowing it was recommended by Chris and Barry)
jmprieur commentedon Apr 16, 2020
Bogdan Gavril (@bgavrilMS) Peter (@pmaytak) you might want to review the design discussed with jennyf19 yesterday ?
bgavrilMS commentedon Apr 16, 2020
The proposal looks fine to me.
I don't have enough context to understand the lifetime of these objects, so please ensure that only 1 factory is ever used.
jmprieur commentedon Apr 16, 2020
I think that's the principle in ASP.NET Core. Thanks Bogdan Gavril (@bgavrilMS)
pmaytak commentedon Apr 21, 2020
Strategy makes sense to me so far and seems useful. I'd have to look closer through the docs to give a more informed opinion.
jmprieur commentedon May 7, 2020
Fixed in Microsoft.Identity.Web 0.1.2-preview