【问题标题】:KB4344167 security update breaks TLS CodeKB4344167 安全更新破坏了 TLS 代码
【发布时间】:2019-02-17 04:40:19
【问题描述】:

希望有人可以帮助解决这个问题。最近我们的机器更新了 KB4344167,其中包括 .NET 4.7.1 的安全更新。不幸的是,此更新破坏了我们的 Webrequest 代码。当我们运行下面的代码时,我们得到了这个错误:

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

// Create a request for the URL.        
WebRequest request = WebRequest.Create(url);
//specify to use TLS 1.2 as default connection
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
request.Timeout = int.Parse(configmanager.GetSetting("Webtimeout"));
// Set proxy
request.Proxy = WebRequest.DefaultWebProxy;
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
// Define a cache policy for this request only. 
HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
request.CachePolicy = noCachePolicy;
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

从机器上卸载安全更新后,代码可以正常执行。我们在上面的代码中遗漏了什么吗?这是我唯一能想到的。

非常感谢任何帮助!

【问题讨论】:

  • TLS 1.1 由于安全漏洞而被贬值,您是否尝试过从安全协议中删除该标志?
  • 继续前进,您可能希望将其设为ServicePointManager.SecurityProtocol = SecurityProtocol.SystemDefault(从 4.7 开始),因此您不必在引入和弃用版本时推出新代码。如果主机不是最新的,这可能仍会导致连接问题,但仅靠代码无法解决这个问题。
  • 据我所知,ServicePointManager.SecurityProtocolCreate() 调用期间被访问/复制之后进行更改应该没有明显的效果(在应用任何修复之前或之后)
  • Damien 的评论很贴切:行在它所在的位置,只有应用程序中的 second 请求应该获取设置;在更改之前创建的 Web 请求将复制现有设置。这在实践中可能会或可能不会被注意到,这取决于您的应用程序实际发出请求的方式,但无论如何它仍然不是您想要的。这可能不是这个问题的原因,但它仍然令人困惑。

标签: c# .net-4.7


【解决方案1】:

@Damien_The_Unbeliever 给出了正确答案。最终问题出在 ServicePointManager 和 Webrequest.Create 的顺序上。颠倒这些行,因此在 Webrequest.Create 修复问题之前定义了 ServicePointManager。当我们的服务器迁移到 TLS 1.2 时,我仍然不知道为什么在 Create 之后添加 ServicePointManager 解决了我们原来的问题,但我们现在不用担心这个。

【讨论】:

    【解决方案2】:

    我遇到了类似的事情。看来 MS 在尝试仅启用 TLS 1.2 时可能破坏了某些东西。 https://support.microsoft.com/en-us/help/4458166/applications-that-rely-on-tls-1-2-strong-encryption-experience-connect

    到目前为止,我已经尝试将建议的配置添加到 app.config 中,并且效果很好。不再有 SSL/TLS 错误。

    <runtime> <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=false" /> </runtime>

    注意:我们在选择性修补的服务器上发现了这一点,即它们还没有 MS 修复。我们的开发机器从未发现过问题。

    【讨论】:

      猜你喜欢
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2021-12-13
      相关资源
      最近更新 更多