Skip to content

Commit 9df34eb

Browse files
committed
Updated to add cert auth
1 parent ee508ef commit 9df34eb

File tree

4 files changed

+96
-22
lines changed

4 files changed

+96
-22
lines changed

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ 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

@@ -25,13 +25,26 @@ This section describes how to use device application code to:
2525
* Update reported device twin properties
2626
* Create a desired property update callback handler
2727

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

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

34-
### Connect to a device
32+
Add this `using` statements to use the device library.
33+
34+
```csharp
35+
using Microsoft.Azure.Devices.Client;
36+
```
37+
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
44+
45+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
46+
47+
#### Authenticate using a shared access key
3548

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

@@ -61,6 +74,10 @@ _deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString,
6174
TransportType.Mqtt);
6275
```
6376

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

6683
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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,18 @@ import com.microsoft.azure.sdk.iot.device.*;
3838
import com.microsoft.azure.sdk.iot.device.DeviceTwin.*;
3939
```
4040

41-
### Connect to the device
41+
### Connect a device to IoT Hub
4242

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

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

@@ -61,6 +70,10 @@ To connect to a device:
6170
client.open(true);
6271
```
6372

73+
#### Authenticate using an X.509 certificate
74+
75+
[!INCLUDE [iot-hub-howto-auth-device-cert-java](iot-hub-howto-auth-device-cert-java.md)]
76+
6477
### Retrieve and view a device twin
6578

6679
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ Run this command to install the **azure-iot-device** device SDK on your developm
3535
npm install azure-iot-device --save
3636
```
3737

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

4055
### Choose a transport protocol

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ ms.custom: mqtt, devx-track-python, py-fresh-zinc
1515

1616
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.
1717

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

2034
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 +40,45 @@ This section describes how to create device application code that:
2640
* Retrieves a device twin and examine reported properties
2741
* Patch reported device twin properties
2842

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

31-
### Connect to a device
60+
#### Authenticate using a shared access key
3261

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

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
64+
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.
65+
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client.
3866

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

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)
69+
```python
70+
# Add your IoT hub primary connection string
71+
CONNECTION_STRING = "{Device primary connection string}"
72+
device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
4873

49-
# connect the application to the device
50-
await device_client.connect()
74+
# Connect the client
75+
device_client.connect()
5176
```
5277

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

5584
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)