Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - MQTTnet client can't connect server certificate

I'm using MQTTnet library to connect to my MQTT server that needs a server certificate. The client one is not needed.

I already installed the certificate inside my PC as i found in other post and created the .pfx file to create the certificate but the program doesn't give me any error..it simply doesn't connect to the topic.

This is my example code

        //Create a new MQTT client
        var factory = new MqttFactory();
        var mqttClient = factory.CreateMqttClient();

        var caCert = new X509Certificate(@"C:caserverroot.pfx", "mypsw");
        var url = "mymqtt.com";
        var username = "user";
        var psw = "user";
        var port = 8885;

        var options = new MqttClientOptionsBuilder()
            .WithClientId(Guid.NewGuid().ToString())
            .WithTcpServer(url, port)
            .WithCredentials(username, psw)
            .WithTls(new MqttClientOptionsBuilderTlsParameters()
            {
                AllowUntrustedCertificates = true,
                UseTls = true,
                Certificates = new List<byte[]> { new X509Certificate2(caCert).Export(X509ContentType.Cert) },
                CertificateValidationCallback = delegate { return true; },
                IgnoreCertificateChainErrors = false,
                IgnoreCertificateRevocationErrors = false
            })
            .WithCleanSession()
            .WithProtocolVersion(MQTTnet.Serializer.MqttProtocolVersion.V311)
            .Build();

        // Connecting
        var result = await mqttClient.ConnectAsync(options);

// Subscribe to a topic

        mqttClient.Connected += async (s, e) =>
        {
            Console.WriteLine("### CONNECTED WITH SERVER ###");

            await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("/mytopic").Build());

            Console.WriteLine("### SUBSCRIBED ###");
        };

With all the orther events that i found here: https://github.com/chkr1011/MQTTnet/wiki/Client

Any of you had experience about this library? How to debug it and find the error?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So, I don't know why i was wrong but using the ManagedMqttClient saved my situation.

This is the code that works like a charm

 //Create a new MQTT client
            var mqttClient = new MqttFactory().CreateManagedMqttClient();

            var caCert = new X509Certificate(@"C:cert.pfx", "psw");
            var url = "myurl.com";
            var username = "user";
            var psw = "user";
            var port = 8885;

            var options = new ManagedMqttClientOptionsBuilder()
                .WithAutoReconnectDelay(TimeSpan.FromSeconds(30))
                .WithClientOptions(new MqttClientOptionsBuilder()
                    .WithClientId(Guid.NewGuid().ToString())
                    .WithTcpServer(url, port)
                    .WithCredentials(username, psw)
                    .WithTls(new MqttClientOptionsBuilderTlsParameters()
                    {
                        AllowUntrustedCertificates = false,
                        UseTls = true,
                        Certificates = new List<byte[]> { new X509Certificate2(caCert).Export(X509ContentType.Cert) },
                        CertificateValidationCallback = delegate { return true; },
                        IgnoreCertificateChainErrors = false,
                        IgnoreCertificateRevocationErrors = false
                    })
                    .WithCleanSession()
                    .WithProtocolVersion(MQTTnet.Serializer.MqttProtocolVersion.V311)
                    .Build())
                .Build();


            // Connecting
            await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("$share:mygroup:/mytopic").Build());
            await mqttClient.StartAsync(options);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...