Refactor HttpClient usage and NTLM auth handling
Centralize HttpClient configuration using IHttpClientFactory with a named "Default" client. Move NTLM authentication logic from HttpClientHandler to request-level options, improving testability and aligning with .NET best practices.
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
using MediatR;
|
||||
using FluentValidation;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using ReC.Application.Common.Behaviors;
|
||||
using ReC.Application.Common.Options;
|
||||
using System.Reflection;
|
||||
using FluentValidation;
|
||||
|
||||
namespace ReC.Application;
|
||||
|
||||
@@ -35,7 +35,11 @@ public static class DependencyInjection
|
||||
cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey;
|
||||
});
|
||||
|
||||
services.AddHttpClient();
|
||||
services.AddHttpClient("Default")
|
||||
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||||
{
|
||||
UseDefaultCredentials = false
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -31,16 +31,7 @@ public class InvokeRecActionCommandHandler(
|
||||
{
|
||||
var action = request.Action;
|
||||
|
||||
var handler = new HttpClientHandler();
|
||||
if (action.EndpointAuthType == "NTLM Auth" && !string.IsNullOrWhiteSpace(action.EndpointAuthUsername))
|
||||
{
|
||||
handler.Credentials = new NetworkCredential(
|
||||
action.EndpointAuthUsername,
|
||||
action.EndpointAuthPassword,
|
||||
action.EndpointAuthDomain);
|
||||
}
|
||||
|
||||
using var http = new HttpClient(handler);
|
||||
using var http = clientFactory.CreateClient("Default");
|
||||
|
||||
if (action.RestType is null)
|
||||
throw new DataIntegrityException(
|
||||
@@ -53,7 +44,7 @@ public class InvokeRecActionCommandHandler(
|
||||
.ToHttpMethod()
|
||||
.ToHttpRequestMessage(action.EndpointUri);
|
||||
|
||||
if(action.Body is not null)
|
||||
if (action.Body is not null)
|
||||
{
|
||||
using var reqBody = new StringContent(action.Body);
|
||||
httpReq.Content = reqBody;
|
||||
@@ -100,10 +91,17 @@ public class InvokeRecActionCommandHandler(
|
||||
httpReq.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case "NTLM Auth":
|
||||
// NTLM authentication is configured on the HttpClientHandler before creating the HttpClient.
|
||||
// No additional action is needed here.
|
||||
if (!string.IsNullOrWhiteSpace(action.EndpointAuthUsername))
|
||||
{
|
||||
var credentials = new NetworkCredential(
|
||||
action.EndpointAuthUsername,
|
||||
action.EndpointAuthPassword,
|
||||
action.EndpointAuthDomain);
|
||||
var credentialCache = new CredentialCache { { httpReq.RequestUri!, "NTLM", credentials } };
|
||||
httpReq.Options.Set(new HttpRequestOptionsKey<CredentialCache>("Credentials"), credentialCache);
|
||||
}
|
||||
break;
|
||||
|
||||
case "Digest Auth":
|
||||
|
||||
Reference in New Issue
Block a user