Merge pull request #177 from sbwalker/master

fix leaky resource probem in client-side Blazor related to HttpClient and AuthenticationStateProvider
This commit is contained in:
Shaun Walker 2019-11-18 14:35:51 -05:00 committed by GitHub
commit 4f86eb24d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<AuthenticationState> 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<HttpClient>();
string apiurl = ServiceBase.CreateApiUrl(sitestate.Alias, NavigationManager.Uri, "User") + "/authenticate";
User user = await http.GetJsonAsync<User>(apiurl);