Merge pull request #4039 from sbwalker/dev
add HttpClientFactory support
This commit is contained in:
@ -253,10 +253,7 @@ else
|
|||||||
|
|
||||||
private void Confirm()
|
private void Confirm()
|
||||||
{
|
{
|
||||||
if (PageState.RenderMode == RenderModes.Interactive || ModuleState.RenderMode == RenderModes.Interactive)
|
OnClick();
|
||||||
{
|
|
||||||
DisplayModal();
|
DisplayModal();
|
||||||
}
|
}
|
||||||
OnClick();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ namespace Oqtane.Services
|
|||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
private readonly SiteState _siteState;
|
private readonly SiteState _siteState;
|
||||||
|
private readonly IHttpClientFactory _factory;
|
||||||
|
|
||||||
protected ServiceBase(HttpClient httpClient, SiteState siteState)
|
protected ServiceBase(HttpClient httpClient, SiteState siteState)
|
||||||
{
|
{
|
||||||
@ -23,7 +24,24 @@ namespace Oqtane.Services
|
|||||||
_siteState = siteState;
|
_siteState = siteState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected ServiceBase(IHttpClientFactory factory, SiteState siteState)
|
||||||
|
{
|
||||||
|
_factory = factory;
|
||||||
|
_siteState = siteState;
|
||||||
|
}
|
||||||
|
|
||||||
public HttpClient GetHttpClient()
|
public HttpClient GetHttpClient()
|
||||||
|
{
|
||||||
|
if (_factory != null)
|
||||||
|
{
|
||||||
|
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))
|
if (!_httpClient.DefaultRequestHeaders.Contains(Constants.AntiForgeryTokenHeaderName) && _siteState != null && !string.IsNullOrEmpty(_siteState.AntiForgeryToken))
|
||||||
{
|
{
|
||||||
@ -31,6 +49,7 @@ namespace Oqtane.Services
|
|||||||
}
|
}
|
||||||
return _httpClient;
|
return _httpClient;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// should be used with new constructor
|
// should be used with new constructor
|
||||||
public string CreateApiUrl(string serviceName)
|
public string CreateApiUrl(string serviceName)
|
||||||
|
@ -11,7 +11,6 @@ using System.Threading.Tasks;
|
|||||||
using Microsoft.AspNetCore.Authentication.OAuth;
|
using Microsoft.AspNetCore.Authentication.OAuth;
|
||||||
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Components;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -230,17 +229,14 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||||||
{
|
{
|
||||||
if (!services.Any(x => x.ServiceType == typeof(HttpClient)))
|
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 });
|
var client = new HttpClient(new HttpClientHandler { UseCookies = false });
|
||||||
client.BaseAddress = new Uri(navigationManager.Uri);
|
var httpContextAccessor = provider.GetRequiredService<IHttpContextAccessor>();
|
||||||
|
|
||||||
// set the cookies to allow HttpClient API calls to be authenticated
|
|
||||||
var httpContextAccessor = s.GetRequiredService<IHttpContextAccessor>();
|
|
||||||
if (httpContextAccessor.HttpContext != null)
|
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)
|
foreach (var cookie in httpContextAccessor.HttpContext.Request.Cookies)
|
||||||
{
|
{
|
||||||
client.DefaultRequestHeaders.Add("Cookie", cookie.Key + "=" + cookie.Value);
|
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
|
// IHttpClientFactory for calling remote services via RemoteServiceBase
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user