diff --git a/src/ReC.Application/DependencyInjection.cs b/src/ReC.Application/DependencyInjection.cs index fd0607a..61981e0 100644 --- a/src/ReC.Application/DependencyInjection.cs +++ b/src/ReC.Application/DependencyInjection.cs @@ -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; } diff --git a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs index 4ae67bf..f564b19 100644 --- a/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs +++ b/src/ReC.Application/RecActions/Commands/InvokeRecActionCommand.cs @@ -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("Credentials"), credentialCache); + } break; case "Digest Auth":