.net core MQTTnet:无法连接TLS:Interop+;AppleCrypto+;SslException:错误的协议版本
我是一名新的.NET核心用户,正在尝试学习如何使用连接MacOS Catalina上的托管客户端和TLS 我正在尝试从ASP.NET Core 3后台服务连接到MOSQUITO代理。使用我能够通过TLS成功连接到服务器,使用用户名、密码和服务器证书(CA)文件。因此,我知道MOSQUITO代理配置正确 但是,我无法使用MQTTnet实现这一点.net core MQTTnet:无法连接TLS:Interop+;AppleCrypto+;SslException:错误的协议版本,.net-core,mqttnet,.net Core,Mqttnet,我是一名新的.NET核心用户,正在尝试学习如何使用连接MacOS Catalina上的托管客户端和TLS 我正在尝试从ASP.NET Core 3后台服务连接到MOSQUITO代理。使用我能够通过TLS成功连接到服务器,使用用户名、密码和服务器证书(CA)文件。因此,我知道MOSQUITO代理配置正确 但是,我无法使用MQTTnet实现这一点 using (var fileStream = new FileStream(_Config.Tls.CACerts, FileMode.Open))
using (var fileStream = new FileStream(_Config.Tls.CACerts, FileMode.Open))
using (var memoryStream = new MemoryStream((int)fileStream.Length))
{
fileStream.CopyTo(memoryStream);
_Logger.LogInformation($"Read file stream with length {memoryStream.Length} bytes, trying to connect with options:");
_Logger.LogInformation($"mqtt://{_Config.UserName}:{_Config.Password}/{_Config.Host}:{_Config.Port}");
_MqttOptions = new ManagedMqttClientOptionsBuilder()
.WithAutoReconnectDelay(TimeSpan.FromSeconds(5))
.WithClientOptions(new MqttClientOptionsBuilder()
.WithClientId(Guid.NewGuid().ToString())
.WithCredentials(_Config.UserName, _Config.Password)
.WithTcpServer(_Config.Host, _Config.Port)
.WithTls(
o =>
{
o.UseTls = true;
o.AllowUntrustedCertificates = true;
o.SslProtocol = SslProtocols.Tls12;
#if WINDOWS_UWP
o.Certificates = new List<byte[]>
{
new X509Certificate(memoryStream.ToArray()).Export(X509ContentType.Cert)
};
#else
o.Certificates = new List<X509Certificate>
{
new X509Certificate(memoryStream.ToArray())
};
#endif
o.CertificateValidationHandler = (context) =>
{
_Logger.LogInformation($"SSL POLICY ERRORS {context.SslPolicyErrors.ToString()}");
return true;
};
}
)
.Build())
.Build();
}
使用(var fileStream=newfilestream(_Config.Tls.CACerts,FileMode.Open))
使用(var memoryStream=newmemorystream((int)fileStream.Length))
{
CopyTo(memoryStream);
_Logger.LogInformation($“读取长度为{memoryStream.length}字节的文件流,尝试使用选项:”)连接;
_Logger.LogInformation($“mqtt://{{u-Config.UserName}:{{u-Config.Password}/{{u-Config.Host}:{{u-Config.Port}”);
_MqttOptions=new managedMQTClientOptionsBuilder()
.具有自动重新连接延迟(时间跨度从秒(5))
.WithClientOptions(新的MQTTClientOptions生成器()
.WithClientId(Guid.NewGuid().ToString())
.WithCredentials(_Config.UserName,_Config.Password)
.WithTcpServer(_Config.Host,_Config.Port)
.带(
o=>
{
o、 UseTls=true;
o、 AllowUntrustedCertificates=true;
o、 SslProtocol=SslProtocols.Tls12;
#如果WINDOWS\u UWP
o、 证书=新列表
{
新的X509证书(memoryStream.ToArray()).Export(X509ContentType.Cert)
};
#否则
o、 证书=新列表
{
新的X509证书(memoryStream.ToArray())
};
#恩迪夫
o、 CertificateValidationHandler=(上下文)=>
{
_Logger.LogInformation($“SSL策略错误{context.SslPolicyErrors.ToString()}”);
返回true;
};
}
)
.Build())
.Build();
}
我收到以下例外情况:
MQTTnet.Exceptions.MqttCommunicationException:身份验证失败,请参阅内部异常。
--->System.Security.Authentication.AuthenticationException:身份验证失败,请参阅内部异常。
--->Interop+AppleCrypto+SslException:协议版本错误
---内部异常堆栈跟踪的结束---
位于System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken消息、AsyncProtocolRequest asyncRequest、ExceptionDispatchInfo异常)
在System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken消息,AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStream.StartSendBlob(字节[]传入,Int32计数,AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStream.ProcessReceivedBlob(字节[]缓冲区,Int32计数,AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStream.StartReadFrame(字节[]缓冲区,Int32 readBytes,AsyncProtocolRequest asyncRequest)
位于System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
---来自引发异常的上一个位置的堆栈结束跟踪---
位于System.Net.Security.SslStream.ThrowifeException()处
在System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult-lazyResult)上
位于System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult结果)
位于System.Net.Security.SslStream.EndAuthenticateTaseClient(IAsyncResult asyncResult)
在System.Net.Security.SslStream.c.b_u64_u2(IAsyncResult iar)上
在System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar、Func`2 endFunction、Action`1 endAction、Task`1 Promission、Boolean requiresSynchronization)
---来自引发异常的上一个位置的堆栈结束跟踪---
在MQTTnet.Implementations.mqtttcpcchannel.ConnectAsync(CancellationToken CancellationToken)
在MQTTnet.Implementations.mqtttcpcchannel.ConnectAsync(CancellationToken CancellationToken)
在MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2操作、TimeSpan超时、CancellationToken CancellationToken)
在MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan超时,CancellationToken CancellationToken)
---内部异常堆栈跟踪的结束---
在MQTTnet.Adapter.MqttChannelAdapter.WrapException(异常)
在MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan超时,CancellationToken CancellationToken)
在MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions选项,CancellationToken CancellationToken)
>>[2020-10-02T16:07:03.9254330Z][4][MqttClient][Verbose]:断开连接[Timeout=00:00:10]
>>[2020-10-02T16:07:03.9255750Z][4][MqttClient][Verbose]:已从适配器断开连接。
>>[2020-10-02T16:07:03.9256240Z][4][MqttClient][Info]:已断开连接。
此外,还明确尝试将与protocolversion(MQTTnet.Formatter.MqttProtocolVersion.V311)一起添加到客户端选项生成器中
有人能帮忙吗?
一些关于他们的文档。
安装.NET Core SDK将安装ASP.NET Core HTTPS
将开发证书发送到本地用户证书存储。这个
证书已经过期
dotnet dev-certs https
dotnet dev-certs https --trust