Skip to content

Commit 92f0ec7

Browse files
Merge pull request #292512 from gsteve88/add-cert-device-twins
Updated to add cert authentication
2 parents 3e3d917 + 2a7b7ed commit 92f0ec7

File tree

5 files changed

+109
-33
lines changed

5 files changed

+109
-33
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,11 @@ This article shows you how to develop two types of applications:
3838
3939
## Prerequisites
4040

41-
* **An IoT hub**. Some SDK calls require the IoT Hub primary connection string, so make a note of the connection string.
41+
* An IoT hub
4242

43-
* **A registered device**. Some SDK calls require the device primary connection string, so make a note of the connection string.
43+
* A registered device
4444

45-
* If your application uses the MQTT protocol, make sure that **port 8883** is open in your firewall. The MQTT protocol communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
46-
47-
* Language SDK requirements:
48-
* **.NET SDK** - Requires Visual Studio.
49-
* **Python SDK** - [Python version 3.7 or later](https://www.python.org/downloads/) is recommended. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable.
50-
* **Java** - Requires [Java SE Development Kit 8](/azure/developer/java/fundamentals/). Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8.
51-
* **Node.js** - Requires Node.js version 10.0.x or later.
45+
* If your application uses the MQTT protocol, make sure that port 8883 is open in your firewall. The MQTT protocol communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
5246

5347
:::zone pivot="programming-language-csharp"
5448

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ ms.author: kgremban
77
ms.service: azure-iot-hub
88
ms.devlang: csharp
99
ms.topic: include
10-
ms.date: 07/12/2024
10+
ms.date: 12/31/2024
1111
ms.custom: mqtt, devx-track-csharp, devx-track-dotnet
1212
---
1313

14+
* Requires Visual Studio
15+
1416
## Overview
1517

1618
This article describes how to use the [Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/readme.md) to create device and backend service application code for device twins.
@@ -25,13 +27,26 @@ This section describes how to use device application code to:
2527
* Update reported device twin properties
2628
* Create a desired property update callback handler
2729

28-
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
29-
30-
### Add device NuGet Package
30+
### Required device NuGet package
3131

3232
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
3333

34-
### Connect to a device
34+
Add this `using` statement to use the device library.
35+
36+
```csharp
37+
using Microsoft.Azure.Devices.Client;
38+
```
39+
40+
### Connect a device to IoT Hub
41+
42+
A device app can authenticate with IoT Hub using the following methods:
43+
44+
* Shared access key
45+
* X.509 certificate
46+
47+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
48+
49+
#### Authenticate using a shared access key
3550

3651
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods required to interact with device twins from the device.
3752

@@ -61,6 +76,10 @@ _deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString,
6176
TransportType.Mqtt);
6277
```
6378

79+
#### Authenticate using an X.509 certificate
80+
81+
[!INCLUDE [iot-hub-howto-auth-device-cert-dotnet](iot-hub-howto-auth-device-cert-dotnet.md)]
82+
6483
### Retrieve a device twin and examine properties
6584

6685
Call [GetTwinAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.gettwinasync?#microsoft-azure-devices-client-deviceclient-gettwinasync) to retrieve the current device twin properties. There are many [Twin](/dotnet/api/microsoft.azure.devices.shared.twin?) object [properties](/dotnet/api/microsoft.azure.devices.shared.twin?&#properties) that you can use to access specific areas of the `Twin` JSON data including `Properties`, `Status`, `Tags`, and `Version`.

includes/iot-hub-howto-device-twins-java.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ms.date: 07/20/2024
1111
ms.custom: mqtt, devx-track-java, devx-track-extended-java
1212
---
1313

14+
* Requires [Java SE Development Kit 8](/azure/developer/java/fundamentals/). Make sure you select **Java 8** under **Long-term support** to navigate to downloads for JDK 8.
15+
1416
## Overview
1517

1618
This article describes how to use the [Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java) to create device and backend service application code for device twins.
@@ -38,9 +40,18 @@ import com.microsoft.azure.sdk.iot.device.*;
3840
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
3941
```
4042

41-
### Connect to the device
43+
### Connect a device to IoT Hub
44+
45+
A device app can authenticate with IoT Hub using the following methods:
46+
47+
* Shared access key
48+
* X.509 certificate
49+
50+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
51+
52+
#### Authenticate using a shared access key
4253

43-
To connect to a device:
54+
To connect a device to IoT Hub:
4455

4556
1. Use [IotHubClientProtocol](/java/api/com.microsoft.azure.sdk.iot.device.iothubclientprotocol) to choose a transport protocol. For example:
4657

@@ -61,6 +72,10 @@ To connect to a device:
6172
client.open(true);
6273
```
6374

75+
#### Authenticate using an X.509 certificate
76+
77+
[!INCLUDE [iot-hub-howto-auth-device-cert-java](iot-hub-howto-auth-device-cert-java.md)]
78+
6479
### Retrieve and view a device twin
6580

6681
After opening the client connection, call [getTwin](/java/api/com.microsoft.azure.sdk.iot.service.devicetwin.devicetwin?#com-microsoft-azure-sdk-iot-service-devicetwin-devicetwin-gettwin(com-microsoft-azure-sdk-iot-service-devicetwin-devicetwindevice)) to retrieve the current twin properties into a `Twin` object.

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ms.date: 07/20/2024
1111
ms.custom: mqtt, devx-track-js
1212
---
1313

14+
* Requires Node.js version 10.0.x or later
15+
1416
## Overview
1517

1618
This article describes how to use the [Azure IoT SDK for Node.js](https://github.com/Azure/azure-iot-sdk-node) to create device and backend service application code for device twins.
@@ -27,14 +29,29 @@ This section describes how to use the [azure-iot-device](/javascript/api/azure-i
2729

2830
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
2931

30-
### Install SDK packages
32+
### Install device SDK package
3133

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

3436
```cmd/sh
3537
npm install azure-iot-device --save
3638
```
3739

40+
### Connect a device to IoT Hub
41+
42+
A device app can authenticate with IoT Hub using the following methods:
43+
44+
* X.509 certificate
45+
* Shared access key
46+
47+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
48+
49+
#### Authenticate using an X.509 certificate
50+
51+
[!INCLUDE [iot-hub-howto-auth-device-cert-node](iot-hub-howto-auth-device-cert-node.md)]
52+
53+
#### Authenticate using a shared access key
54+
3855
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 twin data.
3956

4057
### Choose a transport protocol
@@ -245,7 +262,7 @@ This section describes how to create a backend application that:
245262
* Retrieves and updates a device twin
246263
* Creates a device twin query
247264
248-
### Install service SDK packages
265+
### Install service SDK package
249266
250267
Run this command to install **azure-iothub** on your development machine:
251268

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

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,26 @@ ms.date: 07/12/2024
1111
ms.custom: mqtt, devx-track-python, py-fresh-zinc
1212
---
1313

14+
* Python SDK - [Python version 3.7 or later](https://www.python.org/downloads/) is recommended. Make sure to use the 32-bit or 64-bit installation as required by your setup. When prompted during the installation, make sure to add Python to your platform-specific environment variable.
15+
1416
## Overview
1517

1618
This article describes how to use the [Azure IoT SDK for Python](https://github.com/Azure/azure-iot-sdk-python) to create device and backend service application code for device twins.
1719

20+
## Install packages
21+
22+
The **azure-iot-device** library must be installed to create device applications.
23+
24+
```cmd/sh
25+
pip install azure-iot-device
26+
```
27+
28+
The **azure-iot-hub** library must be installed to create backend service applications.
29+
30+
```cmd/sh
31+
pip install azure-iot-hub
32+
```
33+
1834
## Create a device application
1935

2036
Device applications can read and write twin reported properties, and be notified of desired twin property changes that are set by a backend application or IoT Hub.
@@ -26,30 +42,45 @@ This section describes how to create device application code that:
2642
* Retrieves a device twin and examine reported properties
2743
* Patch reported device twin properties
2844

45+
### Device import statement
46+
47+
Add this code to import the `IoTHubDeviceClient` functions from the azure.iot.device SDK.
48+
49+
```python
50+
from azure.iot.device import IoTHubDeviceClient
51+
```
52+
53+
### Connect a device to IoT Hub
54+
55+
A device app can authenticate with IoT Hub using the following methods:
56+
57+
* Shared access key
58+
* X.509 certificate
59+
2960
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
3061

31-
### Connect to a device
62+
#### Authenticate using a shared access key
3263

33-
This section shows how to connect an application to a device using a device primary key that includes a shared access key.
64+
To connect a device to IoT Hub:
3465

35-
To connect an application to a device:
36-
1. Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to add the device connection string
37-
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client to an Azure IoT hub
66+
1. Call [create_from_connection_string](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-connection-string) to add the device primary connection string.
67+
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client.
3868

39-
```python
40-
# import the device client library
41-
import asyncio
42-
from azure.iot.device.aio import IoTHubDeviceClient
69+
For example:
4370

44-
# substitute the device connection string in conn_str
45-
# and add it to the IoTHubDeviceClient object
46-
conn_str = "{IOT hub device connection string}"
47-
device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
71+
```python
72+
# Add your IoT hub primary connection string
73+
CONNECTION_STRING = "{Device primary connection string}"
74+
device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
4875

49-
# connect the application to the device
50-
await device_client.connect()
76+
# Connect the client
77+
device_client.connect()
5178
```
5279

80+
#### Authenticate using an X.509 certificate
81+
82+
[!INCLUDE [iot-hub-howto-auth-device-cert-python](iot-hub-howto-auth-device-cert-python.md)]
83+
5384
### Retrieve a device twin and examine reported properties
5485

5586
You can retrieve and examine device twin information including tags and properties. The device twin information retrieved matches device twin JSON-formatted data that you can view for a device in the Azure portal.

0 commit comments

Comments
 (0)