首页 > 解决方案 > MQTTnet 客户端认证

问题描述

我有以下错误,当尝试使用以下选项连接到服务器上的蚊子代理时,我收到关于私钥的以下错误,但我正在使用服务器,crt 来验证没有分配私钥的服务器。

代码:

    var serverCertificate = new X509Certificate2("ca.crt");
    Debug.WriteLine("+++++" + serverCertificate.HasPrivateKey);

    var mqttClient = new MqttFactory().CreateMqttClient();

    // Create TCP based options using the builder.
    var options = new MqttClientOptionsBuilder()
        .WithClientId("foo")
        .WithTcpServer("192.168.1.126", 8883)
        .WithCredentials("myusername", "mypassword")
        //.WithTls(true)
        .WithTls(true, false, false, serverCertificate.Export(X509ContentType.Cert))
        .WithCleanSession()
        .WithProtocolVersion(MqttProtocolVersion.V311)
        .WithKeepAlivePeriod(TimeSpan.FromSeconds(60))
        .WithCommunicationTimeout(TimeSpan.FromSeconds(30))
        .Build();

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    var connect = await mqttClient.ConnectAsync(options);

错误

图片

任何帮助将不胜感激,谢谢!

标签: c#xamarinxamarin.formsmqtt

解决方案


  1. 我将我的证书存储在/data/user/0/com.companyname.name/files

  2. 使用此命令创建 pfx 文件 openssl pkcs12 -export -in client.crt -inkey client.key -out client.pfx 使用.pfx代替.crt

.pfx从本地文件中读取,byte[]将其用作证书


推荐阅读