【发布时间】: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