Share via

How Do I Use .PublicClientApplicationBuilder]::Create($ApplicationId).WithHttpClientFactory($HttpClientFactory) In PowerShell?

Jamie Brandwood 131 Reputation points
2023-02-17T20:37:37.05+00:00

How Would I Translate This To PowerShell? I Have Worked Out Everything Apart From Building The MsalHttpClientFactory ... I Have PowerShell 7.3.2 Installed And .NET SDK 6 / 7 My Head Is Hurting ... Please Help :-)
I Think I Need To Work Out How To Use Namespace Interfaces With PowerShell But Its Making My Eyes Go Weird

IMsalHttpClientFactory httpClientFactory = new MyHttpClientFactory();

var pca = PublicClientApplicationBuilder.Create(MsalTestConstants.ClientId) 
                                        .WithHttpClientFactory(httpClientFactory)
                                        .Build();
public class HttpFactoryWithProxy : IMsalHttpClientFactory
{
    private static HttpClient _httpClient;

    public HttpFactoryWithProxy()
    {
        // Consider using Lazy<T> 
        if (_httpClient == null) 
        {
            var proxy = new WebProxy
            {
                Address = new Uri($"http://{proxyHost}:{proxyPort}"),
                BypassProxyOnLocal = false,
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential(
                    userName: proxyUserName,
                    password: proxyPassword)
            };

            // Now create a client handler which uses that proxy
            var httpClientHandler = new HttpClientHandler
            {
                Proxy = proxy,
            };

            _httpClient = new HttpClient(handler: httpClientHandler);
        }
    }

    public HttpClient GetHttpClient()
    {
        return _httpClient;
    }
}
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Anonymous
2023-03-01T21:58:52.83+00:00

Hi @Jamie Brandwood , can you post what you have so far for Powershell so I can cross reference?

This is what I have:

$httpClientFactory = New-Object HttpFactoryWithProxy

$pca = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create("MsalTestConstants.ClientId").WithHttpClientFactory($httpClientFactory).Build()
class HttpFactoryWithProxy : [Microsoft.Identity.Client.IMsalHttpClientFactory]
{
    [Microsoft.Identity.Client.IMsalHttpClientFactory]$httpClient

    HttpFactoryWithProxy()
    {
        if (!$httpClient)
        {
            $proxy = New-Object System.Net.WebProxy
            $proxy.Address = [System.Uri]::new("http://$proxyHost:$proxyPort")
            $proxy.BypassProxyOnLocal = $false
            $proxy.UseDefaultCredentials = $false
            $proxy.Credentials = New-Object System.Net.NetworkCredential($proxyUserName, $proxyPassword)

            $httpClientHandler = New-Object System.Net.Http.HttpClientHandler
            $httpClientHandler.Proxy = $proxy

            $httpClient = New-Object System.Net.Http.HttpClient($httpClientHandler)
        }
    }

    [System.Net.Http.HttpClient] GetHttpClient()
    {
        return $httpClient
    }
}

Let me know if this helps you get on the right track.

Best,

James

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.