Reputation: 152
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.
Upvotes: 4
Views: 1667
Reputation: 414
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
Upvotes: 3