© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y 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

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

    var handler = new HttpClientHandler();
    var certificate = new X509Certificate2("Certificates/certificate.pfx", "password");
    handler.ClientCertificates.Add(certificate);
    return handler;
}); 
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
 var httpClient = _httpClientFactory.CreateClient("sslClient");
try
{
  var response = await httpClient.GetAsync(endpoint);
} catch ...
 var httpClient = _httpClientFactory.CreateClient("sslClient");
try
{
  var response = await httpClient.GetAsync(endpoint);
} catch ...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

how to set HttpClientHandler when registering typed HttpClient?
C#CC# / help
13mo ago
✅ DI with HttpClient / HttpClientFactory
C#CC# / help
3y ago
How to use Server Sent Events with HttpClient
C#CC# / help
3y ago
❔ How to do faster HttpClient requests?
C#CC# / help
4y ago