C#C
C#3y ago
Andres

How to use certificate on HttpClient

I'm trying to add a certificate to my http client but on request it gives the exception RemoteCertificateNameMismatch and i do not understand why it fails i followed the instructions on msdn doc.
Code where i create my client

c#
builder.Services.AddHttpClient("sslClient")
    .ConfigurePrimaryHttpMessageHandler(() =>
{

    var handler = new HttpClientHandler();
    var certificate = new X509Certificate2("Certificates/certificate.pfx", "password");
    handler.ClientCertificates.Add(certificate);
    return handler;
}); 

Code where i create it and I do the get request
c#
 var httpClient = _httpClientFactory.CreateClient("sslClient");
try
{
  var response = await httpClient.GetAsync(endpoint);
} catch ...
Was this page helpful?