我正在尝试将现有应用程序移植到 Mono 2.6.7/Linux。 其中一项任务是通过 WebClient 方法从具有无效 SSL 证书的服务器接收数据。
我们在 Windows 下接受所有证书的 .Net 3.5 代码工作正常:
ServicePointManager.ServerCertificateValidationCallback = TrustCertificate;
StreamReader webReader = new StreamReader(webClient.OpenRead(url));
...
private static bool TrustCertificate(object sender, X509Certificate x509Certificate, X509Chain x509Chain, SslPolicyErrors sslPolicyErrors)
{
// all Certificates are accepted
return true;
}
我尝试了其他。在 Mono 中不使用特定的 Mono dll 来实现相同的效果,但总是出现相同的错误:
Error getting response stream (Write: The authentication or decryption has failed.): SendFailure
有什么想法可以解决这个问题吗?
最佳答案
主要原因是 Mono 与 Microsoft 的 .NET 实现不同,不包含受信任的根证书,因此默认情况下所有证书验证都会失败。
此页面很好地解释了 Mono 上的证书验证如何工作。它还描述了如何负责任地实现您自己的策略,包括示例代码。
http://www.mono-project.com/UsingTrustedRootsRespectfully
该站点有些旧,并使用 ServicePointManager.CertificatePolicy
属性提供 .NET 2.0 代码。您应该改用较新的、未弃用的 ServicePointManager.ServerCertificateValidationCallback
属性。
关于c# - Mono、WebClient 和无效的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674692/