我无法将我的C#代码连接到AWS IoT MQTT Broker,但是我能够使用AWS MQTT Client连接到MQTT代理。我在我的C#代码(https://www.nuget.org/packages/M2Mqtt)中使用M2MQTT作为MQTT客户端。请注意,.pfx文件是使用openSSL使用从AWS IoT下载的证书和私钥创建的。证书被激活并附加到某个东西上。 rootca.crt是亚马逊的根CA.
我一直在Client.Connect(clientId)
{uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException: 类型异常 ' uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' 被扔了。在 uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive(Byte [] msgBytes, Int32超时)at uPLibrary.Networking.M2Mqtt.MqttClient.Connect(String clientId,String username,String password,Boolean willRetain,Byte willQosLevel, Boolean willFlag,String willTopic,String willMessage,Boolean cleanSession,UInt16 keepAlivePeriod)at uPLibrary.Networking.M2Mqtt.MqttClient.Connect(String clientId)
以下是我的代码
private const string IotEndpoint = "xxvf6ihlpxlxf6.iot.us-east-2.amazonaws.com";
private const int BrokerPort = 8883;
private const string Topic = "dsfds2MQTT/#";
var clientCert = new X509Certificate2("C:\\Program Files (x86)\\GnuWin32\\bin\\XXXX.pfx", "XXX#");
var caCert = X509Certificate.CreateFromCertFile("C:\\Program Files (x86)\\GnuWin32\\bin\\rootca.crt");
// create the client
var client = new MqttClient(IotEndpoint, BrokerPort, true, caCert, clientCert, MqttSslProtocols.TLSv1_2);
//message to publish - could be anything
var message = "Test message";
string clientId = Guid.NewGuid().ToString();
//client naming has to be unique if there was more than one publisher
client.Connect(clientId);
//publish to the topic
client.Publish(Topic, Encoding.UTF8.GetBytes(message));
我还查看了此链接Getting AuthenticationException when connect M2Mqtt.MqttClient to Mosquitto broker with TLS和A call to SSPI failed, see inner exception paho m2mqtt Dot.Net(c#) client SSL/TLS connection 他们通过将.crt转换为.pfx来修复问题,但就我的情况而言是亚马逊根CA,我不确定如何在没有私钥的情况下转换为.pfx。这看起来像是一个身份验证问题,但不确定是什么问题。