Skip to content

Commit afd42a9

Browse files
committed
Added cert auth include file
1 parent 55b6242 commit afd42a9

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: How to connect a device to IoT Hub using a certificate (Python)
3+
titleSuffix: Azure IoT Hub
4+
description: Learn how to connect a device to IoT Hub using a certificate and the Azure IoT Hub SDK for Python.
5+
author: kgremban
6+
ms.author: kgremban
7+
ms.service: iot-hub
8+
ms.devlang: python
9+
ms.topic: include
10+
ms.manager: lizross
11+
ms.date: 12/06/2024
12+
---
13+
14+
Use [create_from_x509_certificate](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-create-from-x509-certificate) to connect a device to IoT Hub using a X.509 certificate.
15+
16+
This example shows certificate input parameter values as local variables for clarity. In a production system, store sensitive input parameters in environment variables or another more secure storage location. For example, use `os.getenv("HOSTNAME")` to read the host name environment variable.
17+
18+
```python
19+
# The Azure IoT hub name
20+
hostname = "xxxxx.azure-devices.net"
21+
22+
# The device that has been created on the portal using X509 CA signing or self-signing capabilities
23+
device_id = "MyDevice"
24+
25+
# The X.509 certificate file name
26+
cert_file = "~/certificates/certs/sensor-thl-001-device.cert.pfx"
27+
key_file = "~/certificates/certs/sensor-thl-001-device.cert.key"
28+
# The certificate pass phrase is optional
29+
pass_phrase = "1234"
30+
31+
x509 = X509(
32+
cert_file,
33+
key_file,
34+
pass_phrase,
35+
)
36+
37+
# The client object is used to interact with your Azure IoT hub.
38+
device_client = IoTHubDeviceClient.create_from_x509_certificate(
39+
hostname=hostname, device_id=device_id, x509=x509
40+
)
41+
42+
# Connect to IoT Hub
43+
await device_client.connect()
44+
```
45+
46+
For more information about certificate authentication, see:
47+
48+
* [Authenticate identities with X.509 certificates](/azure/iot-hub/authenticate-authorize-x509)
49+
* [Create and upload certificates for testing](/azure/iot-hub/tutorial-x509-test-certs)
50+
51+
##### Code samples
52+
53+
For working samples of device X.509 certificate authentication, see the examples whose file names end in X.509 at [Async hub scenarios](https://github.com/Azure/azure-iot-sdk-python/tree/main/samples/async-hub-scenarios).

includes/iot-hub-howto-file-upload-python.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,24 @@ from azure.core.exceptions import AzureError
4545
from azure.storage.blob import BlobClient
4646
```
4747

48-
### Connect to the device
48+
### Connect a device to IoT Hub
4949

50-
To connect to the device:
50+
A device app can authenticate and connect using the following methods:
5151

52-
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.
52+
* X.509 certificate
53+
* Shared access key
5354

54-
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client.
55+
#### Connect using an X.509 certificate
56+
57+
[!INCLUDE [iot-hub-howto-auth-device-cert-python](iot-hub-howto-auth-device-cert-python.md)]
58+
59+
#### Connect using a shared access key
60+
61+
To connect a device to IoT Hub:
62+
63+
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
64+
65+
1. Call [connect](/python/api/azure-iot-device/azure.iot.device.iothubdeviceclient?#azure-iot-device-iothubdeviceclient-connect) to connect the device client
5566

5667
For example:
5768

0 commit comments

Comments
 (0)