0

I tried use MQTTnet to connect mqtt.

But seems not worked, it would show the error message:

Unable to connect the remote server, the request was aborted: Could not create SSL/TLS secure channel.

I also found error message on windows event:

A fatal alert was received from the remote endpoint. The TLS protocol defined fatal alert code is 40.

using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Options;

public override void Run()
{
    var option = new MqttClientOptionsBuilder()
                    .WithWebSocketServer("wss://mymqttserver:443")
                    .WithClientId(Guid.NewGuid().ToString())
                    .WithTls(new MqttClientOptionsBuilderTlsParameters()
                    {
                        AllowUntrustedCertificates = true,
                        UseTls = true,
                        SslProtocol = SslProtocols.Tls12,
                        CertificateValidationCallback = delegate { return true; },
                    })
                    .WithCleanSession()
                    .Build();
    var mqtt = new MqttFactory().CreateMqttClient() as MqttClient;
    mqtt.ConnectAsync(option).Wait();
    
    string convertMsg = JsonConvert.SerializeObject("Mqtt Connect Successfully!!");

    var appMsg = new MqttApplicationMessage();
    appMsg.Payload = Encoding.UTF8.GetBytes(convertMsg);
    appMsg.Topic = "myTopic";
    appMsg.QualityOfServiceLevel = MqttQualityOfServiceLevel.AtLeastOnce;
    appMsg.Retain = false;

    mqttClient.PublishAsync(appMsg).Wait();
}

I also tried connect my mqtt server with third party application.

It can connect successfully, so my mqtt server should be okay.

But I don't know why I can't use c# to connect.

enter image description here

EvaHHHH
  • 293
  • 3
  • 16
  • 1
    As the method is called Connect*Async* I suspect you should await it before you can be sure it actually connected. – Emile Dec 16 '21 at 12:39
  • @Emile thanks for remind. I modified it but seems show another error. – EvaHHHH Dec 16 '21 at 13:30
  • You posted a TLS error which has nothing to do with MQTT or async/await. Blocking asynchronous calls is a very bad idea. The whole point of having async operations is to avoid blocking. Your `Run` method should return `async Task` so you can use `await` – Panagiotis Kanavos Dec 16 '21 at 13:36
  • Post the full exception text, not just the message. The full text contains any inner exceptions and the stack trace with the methods that caused this exception. The error you posted can be caused because you use an unsupported Windows or .NET version that doesn't support TLS1.2, because there's a problem with the server's certificate or because client and server couldn't agree on the encryption to use. The inner exception often explains what's wrong – Panagiotis Kanavos Dec 16 '21 at 13:39
  • For example, almost every service requires TLS1.2 nowadays. .NET Framework 4.5 and older Windows versions didn't support this without some tweaking. .NET Framework 4.6.2 and all *supported* Windows versions (ie 10 and 11) work with TLS 1.2 out of the box – Panagiotis Kanavos Dec 16 '21 at 13:42
  • Or perhaps the server uses a self-signed development certificate which, by definition, isn't trusted by anyone. The solution in this case is to add that certificate in the trusted certificates of your OS. Which OS and .NET version are you using? – Panagiotis Kanavos Dec 16 '21 at 13:43
  • @PanagiotisKanavos I use Windows server 2012 R2, and .Net Framework 4.7 – EvaHHHH Dec 16 '21 at 13:53
  • I noticed that your third party's application screenshot is connecting to the unsecured websocket (ws) instead of the secure one (wss). If it's connecting successfully this way, it may indicates that your server is only listening/expecting unsecured websocket (ws) connections. That would explain the TLS handshake issues on the C# side – Megabit Dec 16 '21 at 16:41

1 Answers1

1

In the line

    .WithWebSocketServer("wss://mymqttserver:443")

You must remove the "wss://" because that is already being specified using the method ".WithWebSocketServer". So, you would have

   .WithWebSocketServer("mymqttserver:443")

Just use the server and the port.

Sarah Lissachell
  • 325
  • 1
  • 4
  • 10