【问题标题】:MQTTnet client certificationMQTTnet 客户端认证
【发布时间】:2018-07-26 09:48:02
【问题描述】:

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

错误

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

【问题讨论】:

  • 你可能应该生成一个客户端证书。
  • @Cheesebaron 我刚刚添加了客户端证书,但 M2MQTT 没有找到私钥,虽然 serverCertificate.HasPrivateKey 变为 true,但 ConnectAsync 没有找到它。

标签: c# xamarin xamarin.forms mqtt


【解决方案1】:
  1. 我将证书存储在/data/user/0/com.companyname.name/files

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2015-09-06
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-30
    相关资源
    最近更新 更多