简体   繁体   English

.NET v4.7 不存在 TLS1.2

[英]TLS1.2 does not exist with .NET v4.7

I'm getting an unhandled exception that says The request was aborted: could not create SSL/TLS secure channel so I'm trying to use Tls1.2 in hopes that it will fix this problem.我收到一个未处理的异常, The request was aborted: could not create SSL/TLS secure channel因此我尝试使用 Tls1.2,希望它能解决此问题。

I read that the default for .NET versions 4.6 and above is TLS1.2我读到 .NET 4.6 及更高版本的默认值是 TLS1.2

I have version 4.7 and have the line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;我有 4.7 版,有一行ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; in my code but there's a red linder under .Tls12 .在我的代码中,但 .Tls12 下有一个红色的.Tls12 Visual Studio says SecurityProtocolTypes does not contain a definition for Tls12 and recommends that I replace .Tls12 with .Tls Visual Studio 表示SecurityProtocolTypes does not contain a definition for Tls12并建议我将.Tls12替换为.Tls

How can I use Tls1.2 in my code?如何在我的代码中使用 Tls1.2?

I should add that I've also tried: ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;我应该补充一点,我也试过: ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; and ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0|0x300 0xc00);ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0|0x300 0xc00);

Did you read Transport Layer Security (TLS) best practices with the .NET Framework ? 您是否已阅读.NET Framework的传输层安全性(TLS)最佳做法

It says: 它说:

To ensure .NET Framework applications remain secure, the TLS version should not be hardcoded. 为确保.NET Framework应用程序保持安全, 应对TLS版本进行硬编码。

.NET Framework applications should use the TLS version the operating system (OS) supports. .NET Framework应用程序应使用操作系统(OS)支持的TLS版本。

And later: 然后:

If your app targets .NET Framework 4.7 or later versions 如果您的应用程序针对.NET Framework 4.7或更高版本

The following sections show how to verify you're not using a specific TLS or SSL version. 以下各节说明如何验证您未使用特定的TLS或SSL版本。

For HTTP networking 对于HTTP网络

ServicePointManager, using .NET Framework 4.7 and later versions, defaults to the OS choosing the best security protocol and version. 使用.NET Framework 4.7和更高版本的ServicePointManager,默认情况下是操作系统选择最佳的安全协议和版本。 To get the default OS best choice, if possible, don't set a value for the SecurityProtocol property. 为了获得默认操作系统的最佳选择,请不要为SecurityProtocol属性设置值。 Otherwise, set it to SystemDefault. 否则,将其设置为SystemDefault。

So if it does not work by default... maybe you are not really using 4.7? 因此,如果默认情况下它不起作用...也许您不是真的使用4.7吗?

Or maybe your operating system is so old it does not support it? 还是您的操作系统太旧了,不支持它?

Or maybe the target does not support it? 还是目标不支持它?

Either way, there should be no need to explicitly set it to 1.2 无论哪种方式,都无需将其显式设置为1.2

@nvoigt answered the question correctly but I thought I would share my recent experience with this seeing that its very similar, but with a twist. @nvoigt 正确回答了这个问题,但我想我会分享我最近的经验,看到它非常相似,但有一点不同。

Backstory: I have an application that was developed targeting .net 3.5 and was built using Visual Studio 2013, Update 1. This was for compatibility for another application.背景故事:我有一个针对 .net 3.5 开发的应用程序,它是使用 Visual Studio 2013 更新 1 构建的。这是为了与另一个应用程序兼容。 In that application, it consumed an endpoint that was updated to use TLS 1.2.在该应用程序中,它使用了一个更新为使用 TLS 1.2 的端点。 Because .net 3.5 does not support TLS 1.2, we had to address this issue.因为 .net 3.5 不支持 TLS 1.2,我们不得不解决这个问题。

Based on the answer that @nvoigt posted (and the rest of the web for that matter) I understood that upgrading the application to target a newer .net framework (4.7.1 in my case) would resolve the problem as long as we were not hard-coding any of these protocols in our application.根据@nvoigt 发布的答案(以及与此相关的网络的其余部分),我明白升级应用程序以针对较新的 .net 框架(在我的情况下为 4.7.1)将解决问题,只要我们不是在我们的应用程序中硬编码任何这些协议。 This would allow the OS to indicate the protocol.这将允许操作系统指示协议。

Problem: Using Visual Studio 2013, Update 1 - I updated the target framework of our app to .net 4.7.1 and verified that the ServicePointManager.SecurityProtocol was not being hard-coded anywhere so it would defer to the OS.问题:使用 Visual Studio 2013,更新 1 - 我将我们应用程序的目标框架更新到 .net 4.7.1 并验证ServicePointManager.SecurityProtocol没有在任何地方进行硬编码,因此它会服从操作系统。 This did not work , no matter how many articles I read on the web saying that it would.这不起作用,无论我在网上阅读了多少文章都说它会起作用

Resolution: Using Visual Studio 2019 - I opened and tested the exact same app that was still targeting .net 4.7.1 with no other changes besides the version of Visual Studio used to compile the project.解决方案:使用 Visual Studio 2019 - 我打开并测试了仍然针对 .net 4.7.1 的完全相同的应用程序,除了用于编译项目的 Visual Studio 版本之外没有其他任何更改。 This DID work!这确实有效!

My Findings: I wanted to eliminate any chance that there was something else in my specific project that may have been causing this issue.我的发现:我想消除我的特定项目中可能导致此问题的其他原因的任何可能性。 To do this I created a brand new windows forms project and in the static void Main() method of the Program class I set the following variable var x = ServicePointManager.SecurityProtocol;为此,我创建了一个全新的windows 窗体项目,并在Program类的static void Main()方法中设置了以下变量var x = ServicePointManager.SecurityProtocol; to see what value was being set by default.查看默认设置的值。 This was the ONLY modification that I made to the new project.这是我对新项目所做的唯一修改。 I then continued to debug the EXACT SAME NEW PROJECT in both Visual Studio 2013 and Visual Studio 2019 (both still targeting .net 4.7.1) and I got the results shown below:然后,我继续在 Visual Studio 2013 和 Visual Studio 2019(均针对 .net 4.7.1)中调试完全相同的新项目,结果如下所示:

Visual Studio 2013 - (targeting .net 4.7.1) Visual Studio 2013 -(针对 .net 4.7.1)

var x = ServicePointManager.SecurityProtocol;
//  ^ x defaults to 'Ssl3 | Tls'

Visual Studio 2019 - (targeting .net 4.7.1) Visual Studio 2019 -(针对 .net 4.7.1)

var x = ServicePointManager.SecurityProtocol;
//  ^ x defaults to 'SystemDefault'

See the below side-by-side screenshot (VS 2013 on the Left, VS 2019 on the Right):请参阅下面的并排屏幕截图(左侧为 VS 2013,右侧为 VS 2019): 在此处输入图片说明

Even if I manually set ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;即使我手动设置ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault; in Visual Studio 2013 it would just not work!在 Visual Studio 2013 中它就行不通了! The only way to make it work in Visual Studio 2013 was to hard-code the protocol like this ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;使其在 Visual Studio 2013 中工作的唯一方法是对协议进行硬编码,如ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; which we all know is not a best practice.我们都知道这不是最佳实践。

Lesson learned: When nothing else seems to work when you know it should, test by compiling your application using a newer version of Visual Studio or make sure the version you're using has the latest updates applied.经验教训:当您知道应该执行其他操作时似乎没有其他工作,请通过使用较新版本的 Visual Studio 编译您的应用程序进行测试,或确保您使用的版本应用了最新更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM