I would say you could do it using MQTTnet but I am having similar issues connecting to aws.
private async Task OnConnectToIoTHubCommandAsync()
{
const string server = "sample.amazonaws.com";
const string clientId = "XXXXXXXXXXKELYG82g";
const string machineId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX";
const string topic = "helloServer" + machineId;
byte[] caCertRawBytes;
byte[] clientCertRawBytes;
using (var stream = await FileSystem.OpenAppPackageFileAsync("AmazonRootCA1.crt"))
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
caCertRawBytes = memoryStream.ToArray();
}
}
using (var stream = await FileSystem.OpenAppPackageFileAsync("Private.pfx"))
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
clientCertRawBytes = memoryStream.ToArray();
}
}
var caCert = new X509Certificate(caCertRawBytes);
var clientCert = new X509Certificate2(clientCertRawBytes, "PfxPassword");
var source = new CancellationTokenSource();
var token = source.Token;
var mqttOptions = new MqttClientOptionsBuilder()
.WithTcpServer(server, 8883)
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
Certificates = new List<X509Certificate> { caCert, clientCert },
AllowUntrustedCertificates = false,
IgnoreCertificateChainErrors = false,
IgnoreCertificateRevocationErrors = false,
})
.Build();
// Create a new MQTT client.
var mqttClient = new MqttFactory().CreateMqttClient();
await mqttClient.ConnectAsync(mqttOptions, token);
await Task.CompletedTask.ConfigureAwait(false);
}
The output is an authentication failed see inner exception but the only thing in there is a System Exception exception and a timeout. I'm posting because my problem seems to be closely related to yours. The pfx file is a combination of the thing cert and the private key.