我无法让用户为他们的服务器创建真正的证书,但我想做一些安全检查.因此,以下内容太轻,因为在我阅读时,没有检查证书.
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
您建议我让客户检查x509证书?鉴于我使用的是.NET语言(c#/ f#).
如果您使用自签名证书,那么您应该期望的唯一错误是根(证书颁发者)上的链错误.我会建议像这样的东西,专门捕获链错误,并让所有其他错误落空.
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback( ValidateRemoteCertificate ); private static bool ValidateRemoteCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors ) { string trustedIssuer = "CN=www.domain.com"; string trustedDomain = "CN=www.domain.com"; bool policyErr = false; switch (policyErrors) { case SslPolicyErrors.None: policyErr |= false; break; case SslPolicyErrors.RemoteCertificateChainErrors: bool chainErr = false; foreach (X509ChainStatus status in chain.ChainStatus) { switch (status.Status) { case X509ChainStatusFlags.NoError: chainErr |= false; break; case X509ChainStatusFlags.UntrustedRoot: if (certificate.Subject != trustedDomain || certificate.Issuer != trustedIssuer) chainErr |= true; else chainErr |= false; break; default: chainErr |= true; break; } } policyErr |= chainErr; break; default: policyErr |= true; break; } return !policyErr; }