How to accept a self-signed SSL certificate in a WCF client?

后端 未结 2 905
礼貌的吻别
礼貌的吻别 2021-01-11 17:15

This may be a stupid question but I just can\'t find the answer.

What I would like to do: I have a WCF service hosted by IIS. It is working perfectly, I can access t

相关标签:
2条回答
  • 2021-01-11 17:39

    Here's the minimum amount of code you need to make WCF client accept an arbitrary certificate. This is not secure. Use for testing only. Don't blame me if this code goes berserk and eats your little kitten.

    ServicePointManager.ServerCertificateValidationCallback +=
                new System.Net.Security.RemoteCertificateValidationCallback(EasyCertCheck);
    

    The call back:

    bool EasyCertCheck(object sender, X509Certificate cert,
            X509Chain chain, System.Net.Security.SslPolicyErrors error)
    {
        return true;
    }
    

    Code shamelessly lifted from the least helpful answer to Is it possible to force the WCF test client to accept a self-signed certificate?

    0 讨论(0)
  • 2021-01-11 17:44

    You can register the certificate yourself. If load the certificate in the client as well, and then register the it as trusted you shouldn't get that warning.

    You need to find a X509CertificateCollection and add the certificate to that collection. I had this kind of problem with a SmtpClient running over Ssl.

    By hooking the System.Net.ServicePointManager.ServerCertificateValidationCallback or implementing System.Net.ICertificatePolicy and identify my own installed certificate as valid/trusted (attached to the System.Net.ServicePointManager.CertificatePolicy).

    This is not WCF stuff per se, but from what I could tell, this should translate to WCF as well. It all depends what WCF is uses under the hood.

    0 讨论(0)
提交回复
热议问题