Merge pull request #4039 from sbwalker/dev
add HttpClientFactory support
This commit is contained in:
@ -253,10 +253,7 @@ else
|
||||
|
||||
private void Confirm()
|
||||
{
|
||||
if (PageState.RenderMode == RenderModes.Interactive || ModuleState.RenderMode == RenderModes.Interactive)
|
||||
{
|
||||
DisplayModal();
|
||||
}
|
||||
OnClick();
|
||||
DisplayModal();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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<NavigationManager>();
|
||||
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<IHttpContextAccessor>();
|
||||
var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
|
||||
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<IHttpContextAccessor>();
|
||||
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();
|
||||
|
||||
|
Reference in New Issue
Block a user