Skip to content

Commit bc46612

Browse files
authored
Adjusting connection for certificate store (#49)
1 parent 8b4c887 commit bc46612

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Azure.Devices.DeviceClient/DeviceClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class DeviceClient : IDisposable
3939
private readonly object _lock = new object();
4040
private Timer _timerTokenRenew;
4141
private readonly X509Certificate _azureRootCACert;
42+
private bool _isCertificate;
4243

4344
/// <summary>
4445
/// Device twin updated event.
@@ -66,6 +67,7 @@ public class DeviceClient : IDisposable
6667
/// <param name="modelId">Azure Plug and Play model ID</param>
6768
public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
6869
{
70+
_isCertificate = false;
6971
_clientCert = null;
7072
_privateKey = null;
7173
_iotHubName = iotHubName;
@@ -85,14 +87,16 @@ public DeviceClient(string iotHubName, string deviceId, string sasKey, MqttQoSLe
8587
/// </summary>
8688
/// <param name="iotHubName">Your Azure IoT Hub fully qualified domain name (example: youriothub.azure-devices.net).</param>
8789
/// <param name="deviceId">The device ID (name of your device).</param>
88-
/// <param name="clientCert">The certificate to connect the device (containing both public and private keys).</param>
90+
/// <param name="clientCert">The certificate to connect the device (containing both public and private keys). Pass null if you are using the certificate store on the device</param>
8991
/// <param name="qosLevel">The default quality of assurance level for delivery for the MQTT messages (defaults to the lowest quality).</param>
9092
/// /// <param name="azureCert">Azure certificate for the connection to Azure IoT Hub</param>
9193
/// /// <param name="modelId">Azure Plug and Play model ID</param>
9294
public DeviceClient(string iotHubName, string deviceId, X509Certificate2 clientCert, MqttQoSLevel qosLevel = MqttQoSLevel.AtMostOnce, X509Certificate azureCert = null, string modelId = null)
9395
{
96+
_isCertificate = true;
9497
_clientCert = clientCert;
95-
_privateKey = Convert.ToBase64String(clientCert.PrivateKey);
98+
// In case we are using the store, the magic should happen automaticall
99+
_privateKey = _clientCert != null ? Convert.ToBase64String(clientCert.PrivateKey) : null;
96100
_iotHubName = iotHubName;
97101
_deviceId = deviceId;
98102
_sasKey = null;
@@ -160,7 +164,7 @@ public bool Open()
160164
}
161165

162166
// Now connect the device
163-
string key = _clientCert == null ? Helper.GetSharedAccessSignature(null, _sasKey, $"{_iotHubName}/devices/{_deviceId}", new TimeSpan(24, 0, 0)) : _privateKey;
167+
string key = _isCertificate ? _privateKey : Helper.GetSharedAccessSignature(null, _sasKey, $"{_iotHubName}/devices/{_deviceId}", new TimeSpan(24, 0, 0));
164168
_mqttc.Connect(
165169
_deviceId,
166170
userName,

0 commit comments

Comments
 (0)