Create and provision a simulated X.509 device using C# device SDK for IoT Hub Device Provisioning Service
These steps show you how to use the Azure IoT Samples for C# to simulate an X.509 device on a development machine running the Windows OS. The sample also connects the simulated device to an IoT Hub using the Device Provisioning Service.
If you're unfamiliar with the process of autoprovisioning, be sure to also review Auto-provisioning concepts. Also make sure you've completed the steps in Set up IoT Hub Device Provisioning Service with the Azure portal before continuing.
The Azure IoT Device Provisioning Service supports two types of enrollments:
- Enrollment groups: Used to enroll multiple related devices.
- Individual Enrollments: Used to enroll a single device.
This article will demonstrate individual enrollments.
The initial device twin state configuration is available only in the standard tier of IoT Hub. For more information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.
Prepare the development environment
Make sure you have the .NET Core 2.1 SDK or later installed on your machine.
gitis installed on your machine and is added to the environment variables accessible to the command window. See Software Freedom Conservancy's Git client tools for the latest version of
gittools to install, which includes the Git Bash, the command-line app that you can use to interact with your local Git repository.
Open a command prompt or Git Bash. Clone the Azure IoT Samples for C# GitHub repo:
git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git
Create a self-signed X.509 device certificate and individual enrollment entry
In this section you, will use a self-signed X.509 certificate, it is important to keep in mind the following:
- Self-signed certificates are for testing only, and should not be used in production.
- The default expiration date for a self-signed certificate is one year.
You will use sample code from the Provisioning Device Client Sample - X.509 Attestation to create the certificate to be used with the individual enrollment entry for the simulated device.
In a command prompt, change directories to the project directory for the X.509 device provisioning sample.
The sample code is set up to use X.509 certificates stored within a password-protected PKCS12 formatted file (certificate.pfx). Additionally, you need a public key certificate file (certificate.cer) to create an individual enrollment later in this Quickstart. To generate a self-signed certificate and its associated .cer and .pfx files, run the following command:
The script prompts you for a PFX password. Remember this password, you must use it when you run the sample.
Sign in to the Azure portal, click on the All resources button on the left-hand menu and open your provisioning service.
On the Device Provisioning Service summary blade, select Manage enrollments. Select Individual Enrollments tab and click the Add individual enrollment button at the top.
Under the Add Enrollment panel, enter the following information:
Select X.509 as the identity attestation Mechanism.
Under the Primary certificate .pem or .cer file, click Select a file to select the certificate file certificate.cer created in the previous steps.
Leave Device ID blank. Your device will be provisioned with its device ID set to the common name (CN) in the X.509 certificate, iothubx509device1. This will also be the name used for the registration ID for the individual enrollment entry.
Optionally, you may provide the following information:
- Select an IoT hub linked with your provisioning service.
- Update the Initial device twin state with the desired initial configuration for the device.
Once complete, click the Save button.
On successful enrollment, your X.509 enrollment entry appears as iothubx509device1 under the Registration ID column in the Individual Enrollments tab.
Provision the simulated device
From the Overview blade for your provisioning service, note down the ID Scope value.
Type the following command to build and run the X.509 device provisioning sample. Replace the
<IDScope>value with the ID Scope for your provisioning service.
dotnet run <IDScope>
When prompted, enter the password for the PFX file that you created previously. Notice the messages that simulate the device booting and connecting to the Device Provisioning Service to get your IoT hub information.
Verify that the device has been provisioned. On successful provisioning of the simulated device to the IoT hub linked with your provisioning service, the device ID appears on the hub's Iot Devices blade.
If you changed the initial device twin state from the default value in the enrollment entry for your device, it can pull the desired twin state from the hub and act accordingly. For more information, see Understand and use device twins in IoT Hub
Clean up resources
If you plan to continue working on and exploring the device client sample, do not clean up the resources created in this Quickstart. If you do not plan to continue, use the following steps to delete all resources created by this Quickstart.
- Close the device client sample output window on your machine.
- Close the TPM simulator window on your machine.
- From the left-hand menu in the Azure portal, click All resources and then select your Device Provisioning service. At the top of the All resources blade, click Delete.
- From the left-hand menu in the Azure portal, click All resources and then select your IoT hub. At the top of the All resources blade, click Delete.
In this Quickstart, you’ve created a simulated X.509 device on your Windows machine and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service on the portal. To learn how to enroll your X.509 device programmatically, continue to the Quickstart for programmatic enrollment of X.509 devices.