我是一个新的 .NET 核心用户,试图学习如何使用MQTTnet在 MacOS Catalina 上将托管客户端与 TLS 连接起来。
我正在尝试从 ASP.NET Core 3 后台服务连接到 Mosquitto 代理。使用MqttExplorer,我可以使用用户名、密码和服务器证书 (CA) 文件通过 TLS 成功连接到服务器。所以,我知道 Mosquitto Broker 配置正确。
但是,我无法通过 MQTTnet 实现这一点。
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();
}
我收到以下异常:
MQTTnet.Exceptions.MqttCommunicationException: Authentication failed, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> Interop+AppleCrypto+SslException: bad protocol version
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Security.SslStream.ThrowIfExceptional()
at System.Net.Security.SslStream.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__64_2(IAsyncResult iar)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken)
at MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2 action, TimeSpan timeout, CancellationToken cancellationToken)
at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception)
at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken)
at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken)
>> [2020-10-02T16:07:03.9254330Z] [4] [MqttClient] [Verbose]: Disconnecting [Timeout=00:00:10]
>> [2020-10-02T16:07:03.9255750Z] [4] [MqttClient] [Verbose]: Disconnected from adapter.
>> [2020-10-02T16:07:03.9256240Z] [4] [MqttClient] [Info]: Disconnected.
此外,明确尝试添加WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
到客户端选项生成器。
有人能帮忙吗?