Skip to content

HttpClient not sending certificate setup in handler #109050

@eeaquino

Description

@eeaquino

Description

When going from .Net 8 to .Net 9 and setting up a Named HttpClient for DI with a handler the certificate is never sent. Verified the certificate is never sent by using WireShark to intercept the network communications.

Reproduction Steps

.Net 8

services.AddHttpClient("NamedClient",
        client => { client.BaseAddress = new Uri("serverUrl"); })
    .ConfigurePrimaryHttpMessageHandler(() =>
        {
            var handler = new HttpClientHandler
            {
                ClientCertificateOptions = ClientCertificateOption.Manual
            };
 
            var certs = new X509Certificate2(
                configuration.GetValue<string>("CertPath")!,
                configuration.GetValue<string>("CertPassword")!, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
            handler.ClientCertificates.Add(certs);
            return handler;
        }
    )

.Net 9, Also tired with the same Obsolete code using new X509Certificate2 with the same result.

services.AddHttpClient("NamedClient",
        client => { client.BaseAddress = new Uri("serverUrl"); })
    .ConfigurePrimaryHttpMessageHandler(() =>
        {
            var handler = new HttpClientHandler
            {
                ClientCertificateOptions = ClientCertificateOption.Manual
            };
 
            var certs =X509CertificateLoader.LoadPkcs12FromFile(
                configuration.GetValue<string>("CertPath")!,
                configuration.GetValue<string>("CertPassword")!, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
            handler.ClientCertificates.Add(certs);
            return handler;
        }
    )

Expected behavior

Certificate is sent during handshake.

Image

Actual behavior

No certificate sent

Image

Regression?

Worked as Expected .Net 8

Known Workarounds

No response

Configuration

.Net 9 RC2
Windows 11 x64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions