I'm unable to connect my C# code to AWS IoT MQTT Broker, however I'm able to connect using AWS MQTT Client to MQTT broker. I'm using M2MQTT as the MQTT Client in my C# code (https://www.nuget.org/packages/M2Mqtt). Note that .pfx file is created using openSSL using the certificate and private key downloaded from AWS IoT. The certificate is activated and attached to a thing. The rootca.crt is Amazon's root CA.
I keep getting error at Client.Connect(clientId)
{uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException: Exception of type 'uPLibrary.Networking.M2Mqtt.Exceptions.MqttCommunicationException' was thrown. at uPLibrary.Networking.M2Mqtt.MqttClient.SendReceive(Byte[] msgBytes, Int32 timeout) 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)
Below is my code
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));
I also looked at this link Getting AuthenticationException when connect M2Mqtt.MqttClient to Mosquitto broker with TLS and A call to SSPI failed, see inner exception paho m2mqtt Dot.Net(c#) client SSL/TLS connection where they fixed the issue by converting .crt to .pfx but in my case its Amazon Root CA , I'm not sure how I can convert to .pfx without private key. This looks like an authentication issue but not sure what is wrong.