Skip to content

Commit f3a90d5

Browse files
committed
Add certificate authentication steps to how-to module twins
1 parent 41dcef1 commit f3a90d5

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

articles/iot-hub/how-to-module-twins.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ manager: lizross
88
ms.service: azure-iot-hub
99
ms.devlang: csharp
1010
ms.topic: how-to
11-
ms.date: 09/03/2024
11+
ms.date: 01/03/2025
1212
zone_pivot_groups: iot-hub-howto-c2d-2
1313
ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
1414
---

includes/iot-hub-howto-auth-device-cert-node.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@ The X.509 certificate is attached to the device-to-IoT Hub connection transport.
1515

1616
To configure a device-to-IoT Hub connection using an X.509 certificate:
1717

18-
1. Call [fromConnectionString](/javascript/api/azure-iothub/client?#azure-iothub-client-fromconnectionstring) to add the device connection string and transport type. Add `x509=true` to the device connection string to indicate that a certificate is added to `DeviceClientOptions`. For example: `HostName=xxxxx.azure-devices.net;DeviceId=Device-1;SharedAccessKey=xxxxxxxxxxxxx;x509=true`.
18+
1. Call [fromConnectionString](/javascript/api/azure-iothub/client?#azure-iothub-client-fromconnectionstring) to add the device or module identity twin connection string and transport type to the `Client` object. Add `x509=true` to the device or module connection string to indicate that a certificate is added to `DeviceClientOptions`. For example:
19+
20+
* A device connection string: `HostName=xxxxx.azure-devices.net;DeviceId=Device-1;SharedAccessKey=xxxxxxxxxxxxx;x509=true`
21+
* A module identity twin connection: `HostName=xxxxx.azure-devices.net;DeviceId=Device-1;ModuleId=Module-1;SharedAccessKey=xxxxxxxxxxxxx;x509=true`
22+
1923
1. Configure a JSON variable with certificate details and pass it to [DeviceClientOptions](/javascript/api/azure-iot-device/deviceclientoptions).
2024
1. Call [setOptions](/javascript/api/azure-iot-device/client?#azure-iot-device-client-setoptions-1) to add an X.509 certificate and key (and optionally, passphrase) to the client transport.
2125
1. Call [open](/javascript/api/azure-iothub/client?#azure-iothub-client-open) to open the connection from the device to IoT Hub.
2226

2327
This example shows certificate configuration information within a JSON variable. The certification configuration `options` are passed to `setOptions` and the connection is opened using `open`.
2428

2529
```javascript
26-
var options = {
30+
const Client = require('azure-iot-device').Client;
31+
const Protocol = require('azure-iot-device-mqtt').Mqtt;
32+
// Connection string illustrated for demonstration only. Never hard-code the connection string in production. Instead use an environmental variable or other secure storage.
33+
const connectionString = `HostName=xxxxx.azure-devices.net;DeviceId=Device-1;SharedAccessKey=xxxxxxxxxxxxx;x509=true`
34+
const client = Client.fromConnectionString(connectionString, Protocol);
35+
36+
var clientOptions = {
2737
cert: myX509Certificate,
2838
key: myX509Key,
2939
passphrase: passphrase,
@@ -33,7 +43,8 @@ var options = {
3343
}
3444
}
3545
}
36-
client.setOptions(options, callback);
46+
47+
client.setOptions(clientOptions);
3748
client.open(connectCallback);
3849
```
3950

includes/iot-hub-howto-module-twins-dotnet.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.author: kgremban
77
ms.service: iot-hub
88
ms.devlang: csharp
99
ms.topic: include
10-
ms.date: 11/19/2024
10+
ms.date: 1/3/2025
1111
ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
1212
---
1313

@@ -55,6 +55,9 @@ private static ModuleClient _moduleClient = null;
5555
_moduleClient = ModuleClient.CreateFromConnectionString(ModuleConnectionString, null);
5656
```
5757

58+
> [!NOTE]
59+
> C#/.NET does not support connecting a device app to an IoT Hub module identity twin using a certificate.
60+
5861
### Retrieve a module identity twin and examine properties
5962

6063
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.moduleclient.gettwinasync?#microsoft-azure-devices-client-moduleclient-gettwinasync) to retrieve the current module identity twin properties into a [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object.

includes/iot-hub-howto-module-twins-node.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.author: kgremban
77
ms.service: iot-hub
88
ms.devlang: nodejs
99
ms.topic: include
10-
ms.date: 11/19/2024
10+
ms.date: 1/3/2025
1111
ms.custom: mqtt, devx-track-js
1212
---
1313

@@ -25,19 +25,28 @@ This section describes how to use the [azure-iot-device](/javascript/api/azure-i
2525
* Update module identity reported twin properties
2626
* Receive notice of module identity twin desired property changes
2727

28-
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
28+
The [azure-iot-device](/javascript/api/azure-iot-device) package contains objects that interface with IoT devices. The [Twin](/javascript/api/azure-iot-device/twin) class includes twin-specific objects. This section describes `Client` class code that is used to read and write device module identity twin data.
2929

30-
### Install SDK packages
30+
### Install SDK package
3131

3232
Run this command to install the **azure-iot-device** device SDK on your development machine:
3333

3434
```cmd/sh
3535
npm install azure-iot-device --save
3636
```
3737

38-
The [azure-iot-device](/javascript/api/azure-iot-device) package contains objects that interface with IoT devices. The [Twin](/javascript/api/azure-iot-device/twin) class includes twin-specific objects. This section describes `Client` class code that is used to read and write device module identity twin data.
38+
### Connect a device to IoT Hub
39+
40+
A device app can authenticate with IoT Hub using the following methods:
41+
42+
* Shared access key
43+
* X.509 certificate
3944

40-
### Choose a transport protocol
45+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
46+
47+
#### Authenticate using a shared access key
48+
49+
##### Choose a transport protocol
4150

4251
The `Client` object supports these protocols:
4352

@@ -57,7 +66,7 @@ npm install azure-iot-device-amqp --save
5766

5867
For more information about the differences between MQTT, AMQP, and HTTPS support, see [Cloud-to-device communications guidance](../articles/iot-hub/iot-hub-devguide-c2d-guidance.md) and [Choose a device communication protocol](../articles/iot-hub/iot-hub-devguide-protocols.md).
5968

60-
### Create a client object
69+
##### Create a client object
6170

6271
Create a `Client` object using the installed package.
6372

@@ -67,7 +76,7 @@ For example:
6776
const Client = require('azure-iot-device').Client;
6877
```
6978

70-
### Create a protocol object
79+
##### Create a protocol object
7180

7281
Create a `Protocol` object using an installed transport package.
7382

@@ -77,7 +86,7 @@ This example assigns the AMQP protocol:
7786
const Protocol = require('azure-iot-device-amqp').Amqp;
7887
```
7988

80-
### Add the device connection string and transport protocol
89+
##### Add the device connection string and transport protocol
8190

8291
Call [fromConnectionString](/javascript/api/azure-iot-device/client?#azure-iot-device-client-fromconnectionstring) to supply device connection parameters:
8392

@@ -92,7 +101,7 @@ const Protocol = require('azure-iot-device-mqtt').Amqp;
92101
let client = Client.fromConnectionString(deviceConnectionString, Protocol);
93102
```
94103

95-
### Open the connection to IoT Hub
104+
##### Open the connection to IoT Hub
96105

97106
Use the [open](/javascript/api/azure-iot-device/client?#azure-iot-device-client-open) method to open a connection between an IoT device and IoT Hub.
98107

@@ -107,6 +116,10 @@ client.open(function(err) {
107116
})
108117
```
109118

119+
#### Authenticate using an X.509 certificate
120+
121+
[!INCLUDE [iot-hub-howto-auth-device-cert-node](iot-hub-howto-auth-device-cert-node.md)]
122+
110123
### Retrieve a module identity twin and examine reported properties
111124

112125
Call [getTwin](/javascript/api/azure-iot-device/client?#azure-iot-device-client-gettwin-1) to retrieve current module identity twin information into a [Twin](/javascript/api/azure-iot-device/twin) object.

includes/iot-hub-howto-module-twins-python.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
8080
await device_client.connect()
8181
```
8282

83+
> [!NOTE]
84+
> Python does not support connecting a device app to an IoT Hub module identity twin using a certificate.
85+
8386
### Retrieve a module identity twin and examine properties
8487

8588
Call [get_twin](/python/api/azure-iot-device/azure.iot.device.iothubmoduleclient?#azure-iot-device-iothubmoduleclient-get-twin) to retrieve the module identity twin from the Azure IoT Hub service. The twin information is placed into a variable that can be examined.

0 commit comments

Comments
 (0)