How to authenticate IoT device using X509Certificate

0

I was following this tutorial on 'Azure IoT hub using the X.509 Certificate Authentication'. The sample console app works fine and I was able to send a message to my existing IoThub. However, when I try to connect with my existing IoThub from my UWP using the same code, the same certificate and from the same device, I keep getting non specific exception Message = "Exception from HRESULT: 0x80072F0C". I was wondering what is the basic difference between UWP and console app when connecting with IoT hub using X.509 Certificate.

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, "thumbprint", false);
if (certs.Count > 0 && certs[0].HasPrivateKey)
{
    var auth = new DeviceAuthenticationWithX509Certificate("deviceId", "X509Certificate2");
    deviceClient = DeviceClient.Create("***.azure-devices.net", auth, TransportType.Amqp);

    var message = new Message(Encoding.ASCII.GetBytes("Hello"));
    await deviceClient.SendEventAsync(message);
    outMessage = await ReceiveCloudMessageAsync();
}
else
{
     throw new Exception("cert not found");
}

I was able to get the self-signed cert and has the private key but an exception is being thrown at 'SendEventAsync'.

c#
uwp
iot
azure-iot-hub
x509certificate2
asked on Stack Overflow Jan 11, 2019 by Mike • edited Jan 11, 2019 by Dalton Cézane

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0