如何在c#中使用M2mqtt libraby进行认证?

时间:2017-10-11 09:19:47

标签: c# mqtt hivemq

您好我正在使用M2Mqtt C#库,我希望通过安全性连接到hive MQ代理。 Hive MQ是ssl启用的,我编辑了config.xml以使用java密钥库(.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);

这是需要以下参数的库。

我有root.crt,我在连接时传递给CA.crt。 我应该为客户端证书上传什么,并且X509格式的.crt格式是正确的。

//经过一番尝试后我得到了以下

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;
}

0 个答案:

没有答案