gpt4 book ai didi

c# - 无法在 MqttNet 托管客户端中添加客户端证书

转载 作者:行者123 更新时间:2023-12-03 23:49:32 26 4
gpt4 key购买 nike

我正在使用 MqttNet在我的应用程序中与提供的 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) },

错误信息

enter image description here

我被困在这里两天了。需要帮忙。

最佳答案

导出功能似乎将您的证书转换为 byte[],其中 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

关于c# - 无法在 MqttNet 托管客户端中添加客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859216/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com