MQTTnet客户认证

时间:2018-07-26 09:48:03

标签: c# xamarin xamarin.forms mqtt

我有以下错误,当尝试使用以下选项连接到服务器上的mosquitto代理时,我收到有关私钥的以下错误,但是我正在使用服务器,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);

错误

image

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

1 个答案:

答案 0 :(得分:0)

  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[]将其用作证书