Search code examples
mqttmqttnet

Error while authenticating. Unexpected Maximum QoS value: 2


I have these options to connect to my own broker (CrystalMQ):

_options = new MqttClientOptionsBuilder()
                    .WithClientId(ClientID)
                    .WithTcpServer(Globals.BROKER_ADDRESS, Globals.BROKER_PORT)
                    .WithCredentials(Globals.BROKER_USER, Globals.BROKER_PASSWORD) 
                    .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500) // Use MQTT version 5.0
                    .WithTlsOptions(o =>
                    {
                        o.UseTls(true);
                        o.WithAllowUntrustedCertificates(false);
                        o.WithIgnoreCertificateChainErrors(false);
                        o.WithIgnoreCertificateRevocationErrors(false);

                        o.WithCertificateValidationHandler(_ => true);

                        Assembly assembly = Assembly.GetExecutingAssembly();

                        byte[]? certPubicKeyData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.client.key")?.CopyTo(mem);

                            certPubicKeyData = mem.ToArray();
                        }

                        byte[]? certClientData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.client.crt")?.CopyTo(mem);

                            certClientData = mem.ToArray();
                        }

                        byte[]? certRootData = null;
                        using (MemoryStream mem = new())
                        {
                            assembly.GetManifestResourceStream("MqttClient.Certs.root.crt")?.CopyTo(mem);

                            certRootData = mem.ToArray();
                        }

                        var rootCert = new X509Certificate2(certRootData);
                        var clientCert = new X509Certificate2(certClientData);
                        //var clientKey= new X509Certificate2(certPubicKeyData);
                        o.WithClientCertificates([rootCert, clientCert]);
                        o.WithSslProtocols(System.Security.Authentication.SslProtocols.Tls12);
                    })
                    .WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
                    .WithWillQualityOfServiceLevel(MqttQualityOfServiceLevel.ExactlyOnce)
                    .WithCleanStart(true)
                    .WithSessionExpiryInterval(Globals.BROKER_SESSION_INTERVAL)
                    .Build();

When I try to connect the Disconnected handler is called with this exception:

Error while authenticating. Unexpected Maximum QoS value: 2

I tried even with MqttQualityOfServiceLevel.AtLeastOnce but no avail. Always the same error with the same number.

Is there a way to solve this without editing MQTTnet source code? (Issue logged on MQTTnet github repo)


Solution

  • Thank you, @jstuardo, for bringing this issue to our attention, and @Brits for your time and effort in troubleshooting it.

    The issue was caused by the CONNACK response incorrectly setting Max QoS to 2, which was not compliant with the MQTT specification, leading MQTTnet to reject the connection. We have now resolved this. The Public FREE MQTT Broker has been updated with the fix. However the downloadable version will take little more time to update as we are in the mid of functional integration.

    We highly appreciate your feedback and are happy to assist you always.