Getting a connection error when using MQTTnet V5 and AWS IoT

0

I'm trying to simply connect to the AWS IoT Mqtt broker and get the following:

"Error while authenticating. Extended authentication handler is not yet supported"

The policies for the thing is set for subscription, connection, receive and publish. I search for some answers but didn't find anything even close to this issue.

Below is the code I'm using, any help would be greatly appreciated.

public async Task MqttConnect()
        {
            try
            {
                // Create a new MQTT client.
                var factory = new MqttFactory();
                var mqttClient = factory.CreateMqttClient();

                var caCert = X509Certificate.CreateFromCertFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"certificates\AmazonRootCA1.pem"));
                var clientCert = new X509Certificate2(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"certificates\Alt-ThingCert.pfx"), "");              
                               
                    

                //This is a helper class to allow verifying a root CA separately from the Windows root store
                rootCertificateTrust = new RootCertificateTrust();
                rootCertificateTrust.AddCert(caCert);

                // Certificate based authentication
                List<X509Certificate> certs = new List<X509Certificate>
                {
                    caCert,
                    clientCert
                };


                //Set things up for our MQTTNet client
                //NOTE: AWS does NOT support will topics or retained messages
                //If you attempt to use either, it will disconnect with little explanation

                MqttClientOptionsBuilderTlsParameters tlsOptions = new MqttClientOptionsBuilderTlsParameters();
                tlsOptions.Certificates = certs;
                tlsOptions.SslProtocol = System.Security.Authentication.SslProtocols.Tls12;
                tlsOptions.UseTls = true;
                tlsOptions.AllowUntrustedCertificates = true;
                tlsOptions.CertificateValidationHandler += rootCertificateTrust.VerifyServerCertificate;

                var options = new MqttClientOptionsBuilder()
                    .WithTcpServer(MQTT_Host, MQTT_Port)
                    .WithClientId(Guid.NewGuid().ToString())
                    .WithTls(tlsOptions)
                    .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
                    .Build();

                await mqttClient.ConnectAsync(options, CancellationToken.None);

                var message = new MqttApplicationMessageBuilder()
                    .WithTopic("HeartBeats")
                    .WithPayload("Hello World")
                    .Build();

                await mqttClient.PublishAsync(message, CancellationToken.None);

                Console.WriteLine("==>message sent");
            }
            catch(Exception ex)
            {
                string msg = ex.Message;
            }
        } 
IoTUser
asked 10 months ago582 views
1 Answer
0

Hi - Have you downloaded the certs and then working off it. It seems some issue with the way it is trying to authenticate to AWS IoT core . You may need the following.

  1. deviceCertPEM
  2. devicePrivateCertPEM
  3. certificateAuthorityCertPEM

You can also test the following

  • Downgrade MQTTnet Version
  • Use an AWS IoT SDK
  • Implement Custom Authentication
profile pictureAWS
EXPERT
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions