我在Windows 10 1903 上,我的 IE 和 Chrome 可以访问 Tls 1.2 网站,无需任何特殊设置。但是当我尝试使用我的 .net 4.7.2 应用程序访问只允许 Tls 1.2 连接的 Web API 时,我的连接被服务器终止。
我必须添加ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;我的代码以允许我的应用程序正确连接到该端点。
但是在做了一些研究之后,我从这个文档中发现,对于 .Net 4.7 及以上版本的默认值ServicePointManager.SecurityProtocol应该是,SecurityProtocolType.SystemDefault并且根据这个文档,Tls 1.2 协议默认是启用的。
所以我跟踪了我的代码,发现ServicePointManager.SecurityProtocol我系统上的默认值是System.Net.SecurityProtocolType.Ssl3 | System.Net.SecurityProtocolType.Tls. 更疯狂的是,当我将协议设置为 时SecurityProtocolType.SystemDefault,出现此错误:
System.Net.Http.HttpRequestException : An error occurred while sending the request.
---- System.Net.WebException : The underlying connection was closed: An unexpected error occurred on a receive.
-------- System.ArgumentException : The specified value is not valid in the 'SslProtocolType' enumeration.
Parameter name: sslProtocolType
Run Code Online (Sandbox Code Playgroud)
我检查了我的系统注册表和 IE 设置,没有任何内容覆盖或禁用 Tls 1.2。
所以我很困惑,想知道我的系统或我的代码是否有问题。
如果您运行的应用程序面向4.6 之前的 .NET 框架,您将收到此错误(即使您实际上是在较新的 .NET FX 上运行它)。您可以通过检查 的值来检查这一点AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName。
具体来说,这将在SystemDefaultTlsVersions注册表值未设置为的情况下抛出1:https :
//referencesource.microsoft.com/#System/net/System/Net/SecureProtocols/_SslState.cs,164
AppContext 开关也无济于事,因为它们仅对从 4.6 开始的框架版本生效。假设您无法更改目标 .NET 版本(示例)并且您不想强制您的用户更改他们的注册表设置,我推荐这样的代码:
if (ServicePointManager.SecurityProtocol != SecurityProtocolType.SystemDefault)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息:https : //docs.microsoft.com/en-us/dotnet/framework/network-programming/tls
小智 5
安装 .NET 4.6 后,您仍然必须输入注册表项才能为早期版本编译 .NET 程序以使用强加密。设置完成后,所有 .NET 代码都将使用较新的 TLS 协议 -
64位
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
Run Code Online (Sandbox Code Playgroud)
32位
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
Run Code Online (Sandbox Code Playgroud)
为 4.6 框架及更高版本编译的 .NET 程序将表现得好像密钥就在那里一样。ServiceEndpointManager.SecurityProtocol 设置可用于显式指定它,因此即使没有注册表也可以使用它。
我们在办公室遇到了这个。我的理解是 SystemDefault 值来自 Windows 注册表。
The DefaultSecureProtocols registry entry can be added in the following path:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp
On x64-based computers, DefaultSecureProtocols must also be added to the Wow6432Node path:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp
The registry value is a DWORD bitmap. The value to use is determined by adding the values corresponding to the protocols desired.
DefaultSecureProtocols Value Protocol enabled
0x00000008 Enable SSL 2.0 by default
0x00000020 Enable SSL 3.0 by default
0x00000080 Enable TLS 1.0 by default
0x00000200 Enable TLS 1.1 by default
0x00000800 Enable TLS 1.2 by default
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4461 次 |
| 最近记录: |