Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

HttpClientBuilderExtensions.cs

using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Domain.Security;

namespace Nop.Web.Framework.Infrastructure.Extensions;

/// 
/// Represents extensions of IHttpClientBuilder
/// 
public static class HttpClientBuilderExtensions
{
    /// 
    /// Configure proxy connection for HTTP client (if enabled)
    /// 
    /// A builder for configuring HttpClient
    public static void WithProxy(this IHttpClientBuilder httpClientBuilder)
    {
        httpClientBuilder.ConfigurePrimaryHttpMessageHandler(provider =>
        {
            var handler = new HttpClientHandler();

            //whether proxy is enabled
            var proxySettings = provider.GetService();
            if (!proxySettings?.Enabled ?? true)
                return handler;

            //configure proxy connection
            var webProxy = new WebProxy($"{proxySettings.Address}:{proxySettings.Port}", proxySettings.BypassOnLocal);
            if (!string.IsNullOrEmpty(proxySettings.Username) && !string.IsNullOrEmpty(proxySettings.Password))
            {
                webProxy.UseDefaultCredentials = false;
                webProxy.Credentials = new NetworkCredential
                {
                    UserName = proxySettings.Username,
                    Password = proxySettings.Password
                };
            }
            else
            {
                webProxy.UseDefaultCredentials = true;
                webProxy.Credentials = CredentialCache.DefaultCredentials;
            }

            //configure HTTP client handler
            handler.UseDefaultCredentials = webProxy.UseDefaultCredentials;
            handler.Proxy = webProxy;
            handler.PreAuthenticate = proxySettings.PreAuthenticate;

            return handler;
        });
    }
}