Windows 10上的.NET 4.8 TLS 1.3问题

问题描述

在Windows 10(版本10.0.19041)上运行的.NET 4.8应用程序,其TLS 1.3使用的是how to enable TLS 1.3 in windows 10注册表,

但是运行以下代码

 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());
            }

产生以下内容-异常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,AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.StartReading(Byte[] buffer,AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ProcessRead(Byte[] buffer,AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.Read(Byte[] buffer,Int32 count)
   at System.Net.Tlsstream.Read(Byte[] buffer,Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer,Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request,Boolean userRetrievedStream,Boolean probeRead)

我发现了与.NET 5.0相关的问题,但是我看不到该问题是否已经解决或将要针对.NET 4.8修复。 https://github.com/dotnet/runtime/issues/1720

解决方法

Transport Layer Security (TLS) best practices with the .NET Framework document issue开始,日期为2020-08-21:

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

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

在.NET Core中,我们最近在即将发布的.NET 5 RC1版本中实现了TLS 1.3支持。您可以在那里试用(当然也可以在支持它的OS版本上)。

>

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