I have an HttpClient which uses a self-signed SSL certificate which is saved on the client computer as a .cer file. To validate the certificate, I use ServicePointManager.ServerCertificateValidationCallback in the following way (not my actual code, just the essence):
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, errors) =>
{
if (errors == SslPolicyErrors.None) return true;
if (cert.Subject == "CN=CertificateCommonName")
return CertificatesMatch(cert, GetSavedCertificateFromFile());
else
return false;
};
However, I noticed that this function only gets called once for any certificate per process (more exactly per AppDomain, probably), which means that once a certificate has been validated, it is stored somewhere in a "valid certificates cache". Is there a way I can simply add my saved certificate to that cache?
I wish I could simply use an HttpClientHandler with my HttpClient and add the certificate to the ClientCertificates property, however I am targeting .NET Framework 4.5, and that feature is only available from .NET Framework 4.7.1, so that is not an option.