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:
2025-12-12 12:01:22 +01:00
parent 8c79b3a156
commit 374365d250
2 changed files with 19 additions and 17 deletions

View File

@@ -1,10 +1,10 @@
using MediatR; using FluentValidation;
using MediatR;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ReC.Application.Common.Behaviors; using ReC.Application.Common.Behaviors;
using ReC.Application.Common.Options; using ReC.Application.Common.Options;
using System.Reflection; using System.Reflection;
using FluentValidation;
namespace ReC.Application; namespace ReC.Application;
@@ -35,7 +35,11 @@ public static class DependencyInjection
cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey; cfg.LicenseKey = configOpt.LuckyPennySoftwareLicenseKey;
}); });
services.AddHttpClient(); services.AddHttpClient("Default")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
UseDefaultCredentials = false
});
return services; return services;
} }

View File

@@ -31,16 +31,7 @@ public class InvokeRecActionCommandHandler(
{ {
var action = request.Action; var action = request.Action;
var handler = new HttpClientHandler(); using var http = clientFactory.CreateClient("Default");
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);
if (action.RestType is null) if (action.RestType is null)
throw new DataIntegrityException( throw new DataIntegrityException(
@@ -53,7 +44,7 @@ public class InvokeRecActionCommandHandler(
.ToHttpMethod() .ToHttpMethod()
.ToHttpRequestMessage(action.EndpointUri); .ToHttpRequestMessage(action.EndpointUri);
if(action.Body is not null) if (action.Body is not null)
{ {
using var reqBody = new StringContent(action.Body); using var reqBody = new StringContent(action.Body);
httpReq.Content = reqBody; httpReq.Content = reqBody;
@@ -100,10 +91,17 @@ public class InvokeRecActionCommandHandler(
httpReq.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth); httpReq.Headers.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
} }
break; break;
case "NTLM Auth": case "NTLM Auth":
// NTLM authentication is configured on the HttpClientHandler before creating the HttpClient. if (!string.IsNullOrWhiteSpace(action.EndpointAuthUsername))
// No additional action is needed here. {
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; break;
case "Digest Auth": case "Digest Auth":