嗨,我正在使用 M2Mqtt C# 库,我想通过安全性连接到 hive MQ 代理。Hive MQ 启用了 ssl,我编辑了 config.xml 以使用 java Key store(.jks) 来启用 ssl。
我正在使用自签名证书。我从我的组织获得了证书,我从中生成了 java 密钥库。
ClientLocal = new MqttClient(IPAddress.Parse("127.0.0.1"), 8883,
true, new X509Certificate(@"C:\Users\310208195\root.crt")
, new X509Certificate(@"C:\Users\310208195\root.crt")
MqttSslProtocols.TLSv1_2);
public MqttClient(IPAddress brokerIpAddress, int brokerPort, bool secure, X509Certificate caCert, X509Certificate clientCert, MqttSslProtocols sslProtocol);
这是需要以下参数的库。
我有连接时传递给 CA.crt 的 root.crt。我应该为客户端证书上传什么,.crt 格式对于 X509 格式是否正确。
//所以经过一番尝试,我得到了以下
ClientLocal = new MqttClient(IPAddress.Parse("127.0.0.1"), 8883, false, new X509Certificate(@"C:\Users\310208195\Documents\root.crt"), new X509Certificate2(), MqttSslProtocols.TLSv1_2);
我留空了,但我得到了exception connecting to broker
。"根据验证程序,远程证书无效。"}
这是MQTT代码
public void Initialize()
{
Client_ID = Guid.NewGuid().ToString();
//ClientLocal.ProtocolVersion = MqttProtocolVersion.Version_3_1;
ClientLocal = new MqttClient(“127.0.0.1”, 8883, true, new X509Certificate(Resources.root), MqttSslProtocols.TLSv1_2); ///root.crt is the certificate
//ClientLocal.Connect(Client_ID, “admin”, “admin”, false, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE,
// true, “drlastwill”, “hey, it’s me DR-service app,please restart me!”, true, 60);
ClientLocal.Connect(Client_ID, “admin”, “admin”);
MessageBox.Show(“Connection has been established succesully “);
ushort byt = ClientLocal.Subscribe(new string[] { “event” }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE }); // QOS -2
ushort byt2 = ClientLocal.Subscribe(new string[] { “report” }, new byte[] { MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE });
ClientLocal.MqttMsgSubscribed += Client_MqttMsgSubscribed;
// ClientLocal.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
}