0

I would like to replicate this Python code sample with MQTTnet.

client = mqtt.Client(str(uuid4()))
current = dirname(__file__)
cerfile = join(current, "rcm_certchain_pem.cer")
keyfile = join(current, "rcm_pem_privkey.pkcs8")
context = create_default_context(Purpose.CLIENT_AUTH)
context.load_cert_chain(cerfile, keyfile)
client.tls_set_context(context)
client.tls_insecure_set(True)
client.connect("192.168.1.X", 1234)

I tried this with MQTTnet.

var manager = new MqttFactory().CreateMqttClient();
var options = new MqttClientOptionsBuilder()
    .WithCleanSession()
    .WithClientId(Guid.NewGuid().ToString())
    .WithTcpServer(address, port)
    .WithTls(new MqttClientOptionsBuilderTlsParameters()
    {
        AllowUntrustedCertificates = true,
        Certificates = new List<X509Certificate>
        {
            new X509Certificate2("Assets/rcm_certchain_pem.cer"),
            new X509Certificate2("Assets/rcm_pem_privkey.pkcs8")
        },
        UseTls = true,
    })
    .WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
    .Build();

await manager.ConnectAsync(options, CancellationToken.None);

I always have this crypto related error.

<Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Cannot find the requested object.
0lan
  • 179
  • 1
  • 14
  • 1
    What have you tried? [The docs](https://github.com/dotnet/MQTTnet/wiki/Client#certificate-based-authentication) provide a sample and there are other questions ([such as](https://stackoverflow.com/questions/65417907/how-to-use-pem-files-in-azure-function-using-mqttnet)) that should help you get started. If you get stuck then show your attempt and its likely someone will help. – Brits Apr 06 '22 at 21:18
  • 1
    We are not a porting service, as @Brits mentioned, you need to try, and if/when you get stuck show us what you've tried. – hardillb Apr 06 '22 at 21:30
  • 1
    But those examples use pfx file and not .cer and .pkcs files. I wonder if it is even possible with mqttnet. – 0lan Apr 07 '22 at 04:20

0 Answers0