Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: missing documentation / sample for using Client Certificates with the new MqttClientOptionsBuilder / WithTlsOptions / WithClientCertificatesProvider #2040

Open
hbertsch opened this issue Jul 10, 2024 · 1 comment
Labels
question It is a question regarding the project

Comments

@hbertsch
Copy link

Describe your question

I upgraded to 4.3.6.1152 coming from 4.2.1.781

Which project is your question related to?

I was using this code to attach client certificates for MQTT client authentication:

var mqttClientOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(
                    connectionInfo.MqttConnectionInformation.MqttUri,
                    connectionInfo.MqttConnectionInformation.MqttPort)
                .WithClientId(connectionInfo.MqttConnectionInformation.ClientId)
                .WithTls(new MqttClientOptionsBuilderTlsParameters
                {
                    UseTls = true,
                    SslProtocol = System.Security.Authentication.SslProtocols.Tls12,
                   
                    // this is one X509Certificate2
                    Certificates = new[] { myClientCertificates },
                    CertificateValidationHandler = delegate { return true; },
                })
                .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
                .Build();

This was throwing the following warnings:

warning CS0618: 'MqttClientOptionsBuilderTlsParameters' is obsolete: 'Use methods from MqttClientOptionsBuilder instead.'
warning CS0618: 'MqttClientOptionsBuilderTlsParameters.Certificates' is obsolete: 'Use CertificatesProvider instead.'
warning CS0618: 'MqttClientOptionsBuilder.WithTls(MqttClientOptionsBuilderTlsParameters)' is obsolete: 'Use WithTlsOptions(... configure) instead.'

I managed to resolve the ssl protocol and validation warning but I do not know how to resolve the issue with the CertificatesProvider. There is no sample or documentation (I could find) on how to do this. Can you please help me out here?

var mqttClientOptions = new MqttClientOptionsBuilder()
                .WithTcpServer(
                    connectionInfo.MqttConnectionInformation.MqttUri,
                    connectionInfo.MqttConnectionInformation.MqttPort)
                .WithClientId(connectionInfo.MqttConnectionInformation.ClientId)
                .WithTlsOptions(opt =>
                {
                    opt.WithSslProtocols(SslProtocols.Tls12);
                    opt.WithCertificateValidationHandler(_ => true);
                    opt.WithClientCertificatesProvider(...???...);
                })
                .WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
                .Build();
            
@hbertsch hbertsch added the question It is a question regarding the project label Jul 10, 2024
@joaquingi
Copy link

Hello, have you found a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question It is a question regarding the project
Projects
None yet
Development

No branches or pull requests

2 participants