Forum Discussion
DamianLB
Aug 07, 2023Copper Contributor
IotHubUnauthorizedAccess only when using X.509 Cert to GetFileUploadSasUriAsync
I'm creating a POC for our company using the .NET SDK for Azure IoT. I'm coming up against a problem when performing an HTTP upload, but only when using an X509 cert. I'm provisioning the dev...
DamianLB
Aug 14, 2023Copper Contributor
For anyone else who has the same issue in the future, this is how I fixed it.
This code
using X509Certificate2 certificate = new X509Certificate2(certPath, storedConfig.CertPassword);
using var security = new SecurityProviderX509Certificate(certificate);
using var auth = new DeviceAuthenticationWithX509Certificate(storedConfig.DeviceName, certificate);
IoTDeviceClient = DeviceClient.Create(storedConfig.AssignedIoTHub, auth, TransportType.Mqtt);
IoTDeviceClient.SetRetryPolicy(new ExponentialBackoff(5, new TimeSpan(0, 1, 30), new TimeSpan(0, 1, 30), new TimeSpan(0, 1, 15)));
IoTDeviceClient.OpenAsync().Wait();
Changed to:
SecureString theSecureString = new NetworkCredential("", storedConfig.CertPassword).SecurePassword;
var cert = new X509Certificate2(certPath, theSecureString);
var auth = new DeviceAuthenticationWithX509Certificate(storedConfig.DeviceName, cert);
IoTDeviceClient = DeviceClient.Create(storedConfig.AssignedIoTHub, auth);
IoTDeviceClient.SetRetryPolicy(new ExponentialBackoff(5, new TimeSpan(0, 1, 30), new TimeSpan(0, 1, 30), new TimeSpan(0, 1, 15)));
IoTDeviceClient.OpenAsync().Wait();
I can now use IoT HTTP upload.