.NET 8 C#中SslStream报“无法确定帧大小或收到损坏帧”问题求助
我有两个部署在不同服务器上的.NET 8 C#应用程序,尝试通过SslStream实现安全TCP通信,但在应用间认证环节遇到错误,希望得到解决指导。
我的消息读取代码如下:
string ReadMessage(SslStream sslStream) { // 读取服务器发送的消息 // 消息结尾以"<EOF>"标记标识 byte[] buffer = new byte[2048]; StringBuilder messageData = new StringBuilder(); int bytes = -1; do { bytes = sslStream.Read(buffer, 0, buffer.Length); // 使用Decoder类将字节转换为UTF8 // 处理跨缓冲区的字符 Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(buffer, 0, bytes)]; decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); // 检查是否到达消息结尾 if (messageData.ToString().Contains("<EOF>", StringComparison.CurrentCulture)) { break; } } while (bytes != 0); return messageData.ToString(); }
执行bytes = sslStream.Read(buffer, 0, buffer.Length)时出现以下错误:
System.Security.Authentication.AuthenticationException: Cannot determine the frame size or a corrupted frame was received.
at System.Net.Security.SslStream.GetFrameSize(ReadOnlySpan1 buffer)
at System.Net.Security.SslStream.EnsureFullTlsFrameAsync[TIOAdapter](CancellationToken cancellationToken, Int32 estimatedSize)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](Memory1 buffer, CancellationToken cancellationToken)
at System.Runtime.CompilerServices.PoolingAsyncValueTaskMethodBuilder1.StateMachineBox1.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Security.SslStream.Read(Byte[] buffer, Int32 offset, Int32 count)
我已确认以下连接和证书信息:
- 协议:Tls12
- 已作为服务器认证:True
- 已加密:True
- 可读取:True
- 可写入:True
内容的提问来源于stack exchange,提问作者codeMonkey

