RemoteCertificateValidationCallback with X509Certificate2

c#

Solution

The X509Certificate2 class has a constructor that takes a X509Certificate as parameter. So you can do this:

RemoteCertificateValidationCallback callback = delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslError) {
    X509Certificate2 certv2 = new X509Certificate2(cert);
    // more code here that sends soap request
    return false;
};

Problem

``` X509Certificate2 certificate = new X509Certificate2(); FileStream fileStream = File.Open(@"C:\openssl\bin\cert_key.p12", FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fileStream.Length]; ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(certificate.ValidateRemoteCertificate); Client.ClientCredentials.ClientCertificate.Certificate = certificate; ``` The problem I have with the above code is that the RemoteCertificateValidationCallback only accepts the older X509Certificate not X509Certificate2 type as a parameter. I need X509Certificate2 because the third-party API that I send SOAP request to requires version 2.

Original source