From d7a27eabbbc42eb03454f36d42c262e8afc5ff62 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 18 Nov 2019 14:34:38 -0500 Subject: [PATCH] fix leaky resource probem in client-side Blazor related to HttpClient and AuthenticationStateProvider --- .../Providers/IdentityAuthenticationStateProvider.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs b/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs index d09025b2..2aa9b618 100644 --- a/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs +++ b/Oqtane.Client/Providers/IdentityAuthenticationStateProvider.cs @@ -4,6 +4,7 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.Extensions.DependencyInjection; using Oqtane.Models; using Oqtane.Services; using Oqtane.Shared; @@ -14,17 +15,19 @@ namespace Oqtane.Providers { private readonly NavigationManager NavigationManager; private readonly SiteState sitestate; + private readonly IServiceProvider provider; - public IdentityAuthenticationStateProvider(NavigationManager NavigationManager, SiteState sitestate) + public IdentityAuthenticationStateProvider(NavigationManager NavigationManager, SiteState sitestate, IServiceProvider provider) { this.NavigationManager = NavigationManager; this.sitestate = sitestate; + this.provider = provider; } public override async Task GetAuthenticationStateAsync() { - // hack: create a new HttpClient rather than relying on the registered service as the AuthenticationStateProvider is initialized prior to NavigationManager ( https://github.com/aspnet/AspNetCore/issues/11867 ) - HttpClient http = new HttpClient(); + // get HttpClient lazily from IServiceProvider as you cannot use standard dependency injection due to the AuthenticationStateProvider being initialized prior to NavigationManager ( https://github.com/aspnet/AspNetCore/issues/11867 ) + var http = provider.GetRequiredService(); string apiurl = ServiceBase.CreateApiUrl(sitestate.Alias, NavigationManager.Uri, "User") + "/authenticate"; User user = await http.GetJsonAsync(apiurl);