【问题标题】:Unable to add a client certificate in MqttNet Managed Client无法在 MqttNet 托管客户端中添加客户端证书
【发布时间】:2020-01-22 20:14:50
【问题描述】:

我正在使用MqttNet Library 连接我的应用程序中提供的 Mqtt 服务器。 我正在使用来自here 的托管 mqttnet 客户端

遇到一个小问题,我无法向客户端添加证书。它给了我类型不匹配的错误。

这是我的代码。

 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);

我在这条线上遇到了错误

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

错误信息

我已经被困在这里两天了。需要帮助。

【问题讨论】:

    标签: c# mqtt


    【解决方案1】:

    Export 函数似乎将您的证书转换为一个字节[],其中 Certificates 是 X509Certificate 的 IEnumerable

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

    应该做的伎俩,这很奇怪,因为查看源代码:

    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; }
    }
    

    证书是明确的和 IEnumerable 字节的 IEnumerable,你确定你的包是正确的/最新的吗?我可以完全离开这里:D

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多