Windows 10 上的 .NET 4.8 TLS 1.3 问题

Ric*_*hez 3 .net windows-10 tls1.3 .net-4.8

在 Windows 10(版本 10.0.19041)上运行的 .NET 4.8 应用程序启用了 TLS 1.3,使用注册表按照如何在 Windows 10 中启用 TLS 1.3

但是运行以下代码:

 try
            {                
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
                using (var client = new WebClient())
                { 
                    var img = client.DownloadData("URL of an image - Only TLS 1.3 at client side - removed for privacy purposes");
                    MemoryStream ms = new MemoryStream(img);
                    Image i = Image.FromStream(ms);
                    i.Save(AppDomain.CurrentDomain.BaseDirectory+"/img1.jpeg");
                }
            }
            catch(Exception ex)
            {
                logger.Log(LogLevel.Error, ex.ToString());
            }
Run Code Online (Sandbox Code Playgroud)

产生以下内容 - 异常 StackTrace:

2020-10-05 12:40:52.4779 ERROR System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Cannot determine the frame size or a corrupted frame was received.
   at System.Net.Security._SslStream.StartFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.TlsStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
Run Code Online (Sandbox Code Playgroud)

我发现了 .NET 5.0 的相关问题,但是我不知道该问题是否已修复或将在 .NET 4.8 中修复 https://github.com/dotnet/runtime/issues/1720

kap*_*siR 6

来自.NET Framework 文档问题的传输层安全 (TLS) 最佳实践,日期为 2020 年 8 月 21 日:

.NET Framework 尚不支持 TLS 1.3。这是我们很快就会开始研究的事情(抄送@wfurt)。最重要的是,要使用 TLS 1.3,我们必须使用新的 Windows API,因此我们必须更改 .NET Framework 中的代码,并且由于 TLS 1.3 的其他要求(这些要求在 .NET Framework 中并不重要),更改相当大。 TLS 1.2 及更低版本)。

另请注意,带有 TLS 1.3(非实验性支持)的 Windows 10 最近才发布,我认为仅在 Windows 10 Insider 版本中(@wfurt 有更多详细信息)。

在 .NET Core 中,我们最近在即将推出的 .NET 5 RC1 版本中实现了 TLS 1.3 支持。您可以在那里尝试一下(当然,在支持它的操作系统版本上)。

.NET 5 RC1已发布,并拥有可用于生产的上线许可证。