简体   繁体   中英

Unable to add a client certificate in MqttNet Managed Client

I'm using MqttNet Library to connect with provided Mqtt Server in my application. I'm using managed mqttnet client from here

getting a little problem I'm unable to add certificate with client. it is giving me error of type mismatch.

this is my code.

 var URL = MqttConfiguration.MqttBrokerAddress;
        var username = MqttConfiguration.MqttClientUserName;
        var password = MqttConfiguration.MqttClientPassword;
        var SSLport = MqttConfiguration.SSLPort;

        var options = new ManagedMqttClientOptionsBuilder()
            .WithAutoReconnectDelay(TimeSpan.FromSeconds(30))
            .WithClientOptions(new MqttClientOptionsBuilder()
                .WithClientId(Guid.NewGuid().ToString())
                .WithTcpServer(URL, SSLport)
                .WithCredentials(username, password)
                //.WithTls( GetMqttClientOptions())
                .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.Formatter.MqttProtocolVersion.V311)
                .Build())
            .Build();


        await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic(Topics.handshake).Build());

        await mqttClient.StartAsync(options);

I'm getting the error when on this line

Certificates = new List<byte[]> { new X509Certificate2(caCert).Export(X509ContentType.Cert) },

Error message

在此处输入图片说明

I've been stuck here from two days. need help.

The Export function seems to transform your certificate to a byte[] where Certificates is an IEnumerable of X509Certificate

Certificates = new List<X509Certificate> { new X509Certificate2(caCert) }

Should do the trick, which is odd because looking at the source code:

public class MqttClientOptionsBuilderTlsParameters
{
    public bool UseTls { get; set; }

    public Func<X509Certificate, X509Chain, SslPolicyErrors, IMqttClientOptions, bool> CertificateValidationCallback
    {
        get;
        set;
    }

    public SslProtocols SslProtocol { get; set; } = SslProtocols.Tls12;

    public IEnumerable<IEnumerable<byte>> Certificates { get; set; }

    public bool AllowUntrustedCertificates { get; set; }

    public bool IgnoreCertificateChainErrors { get; set; }

    public bool IgnoreCertificateRevocationErrors { get; set; }
}

Certificates is clearly and IEnumerable of IEnumerable of byte, are you sure your package is correct/up to date? I could be completely off here :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM