From ce14b9e43e53954a40d9ae4726df55170d07ccc9 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Thu, 21 Mar 2024 10:43:46 -0400 Subject: [PATCH] add HttpClientFactory support --- .../Modules/Controls/ActionDialog.razor | 5 +--- Oqtane.Client/Services/ServiceBase.cs | 25 +++++++++++++++--- .../OqtaneServiceCollectionExtensions.cs | 26 +++++++++++++------ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/Oqtane.Client/Modules/Controls/ActionDialog.razor b/Oqtane.Client/Modules/Controls/ActionDialog.razor index a2a89ba2..f21d5a64 100644 --- a/Oqtane.Client/Modules/Controls/ActionDialog.razor +++ b/Oqtane.Client/Modules/Controls/ActionDialog.razor @@ -253,10 +253,7 @@ else private void Confirm() { - if (PageState.RenderMode == RenderModes.Interactive || ModuleState.RenderMode == RenderModes.Interactive) - { - DisplayModal(); - } OnClick(); + DisplayModal(); } } diff --git a/Oqtane.Client/Services/ServiceBase.cs b/Oqtane.Client/Services/ServiceBase.cs index d23b3e33..4e865de9 100644 --- a/Oqtane.Client/Services/ServiceBase.cs +++ b/Oqtane.Client/Services/ServiceBase.cs @@ -16,6 +16,7 @@ namespace Oqtane.Services { private readonly HttpClient _httpClient; private readonly SiteState _siteState; + private readonly IHttpClientFactory _factory; protected ServiceBase(HttpClient httpClient, SiteState siteState) { @@ -23,13 +24,31 @@ namespace Oqtane.Services _siteState = siteState; } + protected ServiceBase(IHttpClientFactory factory, SiteState siteState) + { + _factory = factory; + _siteState = siteState; + } + public HttpClient GetHttpClient() { - if (!_httpClient.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken)) + if (_factory != null) { - _httpClient.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken); + var client = _factory.CreateClient("oqtane"); + if (!client.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken)) + { + client.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken); + } + return client; + } + else + { + if (!_httpClient.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken)) + { + _httpClient.DefaultRequestHeaders.Add(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken); + } + return _httpClient; } - return _httpClient; } // should be used with new constructor diff --git a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs index 2fd109ba..810e4ecd 100644 --- a/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs +++ b/Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs @@ -11,7 +11,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.OAuth; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; @@ -230,17 +229,14 @@ namespace Microsoft.Extensions.DependencyInjection { if (!services.Any(x => x.ServiceType == typeof(HttpClient))) { - services.AddScoped(s => + services.AddScoped(provider => { - // creating the URI helper needs to wait until the JS Runtime is initialized, so defer it. - var navigationManager = s.GetRequiredService(); var client = new HttpClient(new HttpClientHandler { UseCookies = false }); - client.BaseAddress = new Uri(navigationManager.Uri); - - // set the cookies to allow HttpClient API calls to be authenticated - var httpContextAccessor = s.GetRequiredService(); + var httpContextAccessor = provider.GetRequiredService(); if (httpContextAccessor.HttpContext != null) { + client.BaseAddress = new Uri(httpContextAccessor.HttpContext.Request.Scheme + "://" + httpContextAccessor.HttpContext.Request.Host); + // set the cookies to allow HttpClient API calls to be authenticated foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies) { client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + cookie.Value); @@ -251,6 +247,20 @@ namespace Microsoft.Extensions.DependencyInjection }); } + services.AddHttpClient("oqtane", (provider, client) => + { + var httpContextAccessor = provider.GetRequiredService(); + if (httpContextAccessor.HttpContext != null) + { + client.BaseAddress = new Uri(httpContextAccessor.HttpContext.Request.Scheme + "://" + httpContextAccessor.HttpContext.Request.Host); + // set the cookies to allow HttpClient API calls to be authenticated + foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies) + { + client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + cookie.Value); + } + } + }); + // IHttpClientFactory for calling remote services via RemoteServiceBase services.AddHttpClient();