简体   繁体   English

在 HttpClient C# 中支持 TLS 1.2

[英]supporting TLS 1.2 in HttpClient C#

Good afternoon!下午好! I use Azure Maps API using HttpClient.我使用 HttpClient 使用 Azure Maps API。 How can I enable support of TLS 1.2?如何启用对 TLS 1.2 的支持? As I know in Framework 4.6+ it is supported.据我所知,在 Framework 4.6+ 中它是受支持的。 And I should not do anything for this to work?我不应该为此做任何事情吗?

Use ServicePointManager to set the security protocol.使用ServicePointManager设置安全协议。

Gets or sets the security protocol used by the ServicePoint objects managed by the ServicePointManager object.获取或设置由 ServicePointManager 对象管理的 ServicePoint 对象使用的安全协议。

HttpClient httpClient = new HttpClient();   

//specify to use TLS 1.2 as default connection
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

This property selects the version of the Secure Sockets Layer (SSL) or Transport Layer Security (TLS) protocol to use for new connections;此属性选择用于新连接的安全套接字层 (SSL) 或传输层安全 (TLS) 协议的版本; existing connections aren't changed.现有连接不会改变。

Starting with the .NET Framework 4.7, the default value of this property is SecurityProtocolType.SystemDefault.从 .NET Framework 4.7 开始,此属性的默认值为 SecurityProtocolType.SystemDefault。 This allows .NET Framework networking APIs based on SslStream (such as FTP, HTTP, and SMTP) to inherit the default security protocols from the operating system or from any custom configurations performed by a system administrator.这允许基于 SslStream 的 .NET Framework 网络 API(例如 FTP、HTTP 和 SMTP)从操作系统或系统管理员执行的任何自定义配置继承默认安全协议。

In general you do not need to specify any configuration in your application to enable adoption of the latest TLS protocol.通常,无需在应用程序中指定任何配置即可采用最新的 TLS 协议。

Best practices and scenarios are outlined on docs.microsoft.com for earlier than .Net 4.7 . docs.microsoft.com上概述了 .Net 4.7 之前的最佳实践和方案。

At high level, you should make audit to make sure your application doesn't take any hard dependency on a lower TLS version.在高层次上,您应该进行审核以确保您的应用程序不会对较低的 TLS 版本产生任何硬依赖。 But otherwise no work should be required.但除此之外,不需要任何工作。

We recommend that you:我们建议您:

  • Target .NET Framework 4.7 or later versions on your apps.在您的应用上以 .NET Framework 4.7 或更高版本为目标。 Target .NET Framework 4.7.1 or later versions on your WCF apps.在 WCF 应用程序上定位 .NET Framework 4.7.1 或更高版本。
  • Do not specify the TLS version.不要指定 TLS 版本。 Configure your code to let the OS decide on the TLS version.配置您的代码,让操作系统决定 TLS 版本。
  • Perform a thorough code audit to verify you're not specifying a TLS or SSL version.执行彻底的代码审计以验证您没有指定 TLS 或 SSL 版本。

When your app lets the OS choose the TLS version:当您的应用让操作系统选择 TLS 版本时:

  • It automatically takes advantage of new protocols added in the future, such as TLS 1.3.它会自动利用未来添加的新协议,例如 TLS 1.3。
  • The OS blocks protocols that are discovered not to be secure.操作系统会阻止被发现不安全的协议。

It will be worth exploring Microsoft documentation on the TLS best practice值得探索有关TLS 最佳实践的Microsoft 文档

For me the issue was solved by adding one of the below registry keys:对我来说,问题是通过添加以下注册表项之一解决的:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v2.0.50727]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SystemDefaultTlsVersions"=dword:00000001
"SchUseStrongCrypto"=dword:00000001

The official Microsoft answer available here 此处提供Microsoft 官方答案

It looks like this :它看起来像这样:

private static void Main(string[] args)
{
   var httpClient = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = false, SslProtocols = System.Security.Authentication.SslProtocols.Tls12 });

   ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

   var response = httpClient.GetAsync("http//myurl.com").GetAwaiter().GetResult();

 }

But is not working for all of our projects (including != than 4.7.1 framework projects), please add info if you have但不适用于我们所有的项目(包括 != 4.7.1 框架项目),如果有,请添加信息

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

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