Skip to content

[Feature Request] Leverage the ASP.NET Core IHttpClientFactoryΒ #6

Closed

Description

@jennyf19

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

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

  1. Add a new parameter httpClientFactory of type IHttpClientFactory in the constructor of TokenAcquisition:
    public TokenAcquisition(
    IMsalTokenCacheProvider tokenCacheProvider,
    IHttpContextAccessor httpContextAccessor,
    IOptions<MicrosoftIdentityOptions> microsoftIdentityOptions,
    IOptions<ConfidentialClientApplicationOptions> applicationOptions,
    ILogger<TokenAcquisition> logger)
    so that an HttpClientFactor can be injected by dependency injection. Also add a corresponding property to TokenAcquisition
  2. Add a new internal class (for instance MsalAspNetCoreHttpClientFactory) implementing IMsalHttpClientFactory and having a constructor with an ASP.NET Core IHttpClientFactory, and which CreateClient() method would just delegate to IHttpClientFactory
  3. When building MSAL.NET confidential client application, use the .WithHttpClientFactory(httpClientFactory) modifyer and instanciate a MsalAspNetCoreHttpClientFactory from the httpClientFactory property
  4. In AddWebAppCallsWebApi and AddProtectedWebApiCallsWebApi, 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

  1. jmprieur commented on Feb 25, 2020

    @jmprieur
    Collaborator

    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)

  2. jmprieur commented on Apr 16, 2020

    @jmprieur
    Collaborator

    Bogdan Gavril (@bgavrilMS) Peter (@pmaytak) you might want to review the design discussed with jennyf19 yesterday ?

  3. bgavrilMS commented on Apr 16, 2020

    @bgavrilMS
    Member

    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.

  4. jmprieur commented on Apr 16, 2020

    @jmprieur
    Collaborator

    I think that's the principle in ASP.NET Core. Thanks Bogdan Gavril (@bgavrilMS)

  5. pmaytak commented on Apr 21, 2020

    @pmaytak
    Collaborator

    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.

  6. jmprieur commented on May 7, 2020

    @jmprieur
    Collaborator

    Fixed in Microsoft.Identity.Web 0.1.2-preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions