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;

上面的代码存在的问题是,RemoteCertificateValidationCallback仅接受较旧的X509Certificate,而不接受X509Certificate2类型作为参数。我需要X509Certificate2,因为我发送SOAP请求的第三方API需要版本2。

最佳答案

X509Certificate2类具有一个构造函数,该构造函数将X509Certificate用作参数。因此,您可以执行以下操作:

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;
};

关于c# - X509Certificate2的RemoteCertificateValidationCallback,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2156116/

10-11 21:40