c# - 请求被中止 : Could not create SSL/TLS secure channel

标签 c# asp.net windows-8 windows-7 httpwebrequest

由于此错误消息,我们无法使用 WebRequest 连接到 HTTPS 服务器:

请求被中止:无法创建 SSL/TLS 安全通道。

我们知道服务器没有使用路径的有效 HTTPS 证书,但为了绕过这个问题,我们使用从另一篇 StackOverflow 帖子中获取的以下代码:

private void Somewhere() {
    ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AlwaysGoodCertificate);
}

private static bool AlwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) {
   return true;
}

问题是服务器从不验证证书并因上述错误而失败。有谁知道我应该做什么?


我应该提一下,我和一位同事在几周前进行了测试,它运行良好,与我上面写的类似。我们发现的唯一“主要区别”是我使用的是 Windows 7 而他使用的是 Windows XP。这会改变什么吗?

最佳答案

我终于找到了答案(我没有注明出处,但它来自搜索);

虽然代码在 Windows XP 中有效,但在 Windows 7 中,您必须在开头添加:

// using System.Net;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Use SecurityProtocolType.Ssl3 if needed for compatibility reasons

现在,它完美地工作了。


附录

正如 Robin French 所提到的;如果您在配置 PayPal 时遇到此问题,请注意,从 2018 年 12 月 3 日开始,它们将不再支持 SSL3。您需要使用 TLS。这是 Paypal page关于它。

关于c# - 请求被中止 : Could not create SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980612/

相关文章:

C# 处理十六进制值和二进制转换

c# - 代码隐藏中包含 c# 和 vb.net 的 asp.net Web 应用程序

c# - 如何从 ASP.Net 中的基类访问 session 变量?

ruby-on-rails - Windows 中的 RoR 不适用于在 Linux 中使用 RoR 构建的现有应用程序

c# - 使用计数 c# 按列表排序

c# - ServiceStack OrmLite : Use default database constraint instead of null value from data model

c# - 转换没有特定类型的泛型类

asp.net - RoleProvider 中这两种方法有什么区别?

android - 如何将 phonegap 应用程序直接部署到设备

windows-8 - 跨 WP8 和 Win8 识别用户 : ANID2 vs SafeCustomerId