|
| 1 | +--- |
| 2 | +title: Enroll TPM device to Azure Device Provisioning Service using C# |
| 3 | +description: Quickstart - Enroll TPM device to Azure IoT Hub Device Provisioning Service using C# service SDK. This quickstart uses individual enrollments. |
| 4 | +author: wesmc7777 |
| 5 | +ms.author: wesmc |
| 6 | +ms.date: 11/08/2019 |
| 7 | +ms.topic: quickstart |
| 8 | +ms.service: iot-dps |
| 9 | +services: iot-dps |
| 10 | +ms.devlang: csharp |
| 11 | +ms.custom: mvc |
| 12 | +--- |
| 13 | + |
| 14 | +# Quickstart: Enroll TPM device to IoT Hub Device Provisioning Service using C# service SDK |
| 15 | + |
| 16 | +[!INCLUDE [iot-dps-selector-quick-enroll-device-tpm](../../includes/iot-dps-selector-quick-enroll-device-tpm.md)] |
| 17 | + |
| 18 | +This article shows how to programmatically create an individual enrollment for a TPM device in the Azure IoT Hub Device Provisioning Service by using the [C# Service SDK](https://github.com/Azure/azure-iot-sdk-csharp) and a sample C# .NET Core application. You can optionally enroll a simulated TPM device to the provisioning service by using this individual enrollment entry. Although these steps work on both Windows and Linux computers, this article uses a Windows development computer. |
| 19 | + |
| 20 | +## Prepare the development environment |
| 21 | + |
| 22 | +1. Verify you have [Visual Studio 2019](https://www.visualstudio.com/vs/) installed on your computer. |
| 23 | + |
| 24 | +1. Verify you have the [.NET Core SDK](https://www.microsoft.com/net/download/windows) installed on your computer. |
| 25 | + |
| 26 | +1. Complete the steps in [Set up the IoT Hub Device Provisioning Service with the Azure portal](./quick-setup-auto-provision.md) before you continue. |
| 27 | + |
| 28 | +1. (Optional) If you want to enroll a simulated device at the end of this quickstart, follow the procedure in [Create and provision a simulated TPM device using C# device SDK](quick-create-simulated-device-tpm-csharp.md) up to the step where you get an endorsement key for the device. Save the endorsement key, registration ID, and, optionally, the device ID, because you need to use them later in this quickstart. |
| 29 | + |
| 30 | + > [!NOTE] |
| 31 | + > Don't follow the steps to create an individual enrollment by using the Azure portal. |
| 32 | +
|
| 33 | +## Get the connection string for your provisioning service |
| 34 | + |
| 35 | +For the sample in this quickstart, you need the connection string for your provisioning service. |
| 36 | + |
| 37 | +1. Sign in to the Azure portal, select **All resources**, and then your Device Provisioning Service. |
| 38 | + |
| 39 | +1. Choose **Shared access policies**, then select the access policy you want to use to open its properties. In **Access Policy**, copy and save the primary key connection string. |
| 40 | + |
| 41 | +  |
| 42 | + |
| 43 | +## Create the individual enrollment sample |
| 44 | + |
| 45 | +This section shows how to create a .NET Core console app that adds an individual enrollment for a TPM device to your provisioning service. With some modification, you can also follow these steps to create a [Windows IoT Core](https://developer.microsoft.com/en-us/windows/iot) console app to add the individual enrollment. To learn more about developing with IoT Core, see [Windows IoT Core developer documentation](https://docs.microsoft.com/windows/iot-core/). |
| 46 | + |
| 47 | +1. Open Visual Studio and select **Create a new project**. In **Create a new project**, choose the **Console App (.NET Core)** project template for C# and select **Next**. |
| 48 | + |
| 49 | +1. Name the project *CreateTpmEnrollment*, and select **Create**. |
| 50 | + |
| 51 | +  |
| 52 | + |
| 53 | +1. In **Solution Explorer**, right-click the **CreateTpmEnrollment** project, and then select **Manage NuGet Packages**. |
| 54 | + |
| 55 | +1. In **NuGet Package Manager**, select **Browse**, search for and choose **Microsoft.Azure.Devices.Provisioning.Service**, and then select **Install**. |
| 56 | + |
| 57 | +  |
| 58 | + |
| 59 | + This step downloads, installs, and adds a reference to the [Azure IoT Provisioning Service Client SDK](https://www.nuget.org/packages/Microsoft.Azure.Devices.Provisioning.Service/) NuGet package and its dependencies. |
| 60 | + |
| 61 | +1. Add the following `using` statements after the other `using` statements at the top of `Program.cs`: |
| 62 | + |
| 63 | + ```csharp |
| 64 | + using System.Threading.Tasks; |
| 65 | + using Microsoft.Azure.Devices.Provisioning.Service; |
| 66 | + ``` |
| 67 | + |
| 68 | +1. Add the following fields to the `Program` class, and make the listed changes. |
| 69 | + |
| 70 | + ```csharp |
| 71 | + private static string ProvisioningConnectionString = "{Your provisioning service connection string}"; |
| 72 | + private const string RegistrationId = "sample-registrationid-csharp"; |
| 73 | + private const string TpmEndorsementKey = |
| 74 | + "AToAAQALAAMAsgAgg3GXZ0SEs/gakMyNRqXXJP1S124GUgtk8qHaGzMUaaoABgCAAEMAEAgAAAAAAAEAxsj2gUS" + |
| 75 | + "cTk1UjuioeTlfGYZrrimExB+bScH75adUMRIi2UOMxG1kw4y+9RW/IVoMl4e620VxZad0ARX2gUqVjYO7KPVt3d" + |
| 76 | + "yKhZS3dkcvfBisBhP1XH9B33VqHG9SHnbnQXdBUaCgKAfxome8UmBKfe+naTsE5fkvjb/do3/dD6l4sGBwFCnKR" + |
| 77 | + "dln4XpM03zLpoHFao8zOwt8l/uP3qUIxmCYv9A7m69Ms+5/pCkTu/rK4mRDsfhZ0QLfbzVI6zQFOKF/rwsfBtFe" + |
| 78 | + "WlWtcuJMKlXdD8TXWElTzgh7JS4qhFzreL0c1mI0GCj+Aws0usZh7dLIVPnlgZcBhgy1SSDQMQ=="; |
| 79 | + |
| 80 | + // Optional parameters |
| 81 | + private const string OptionalDeviceId = "myCSharpDevice"; |
| 82 | + private const ProvisioningStatus OptionalProvisioningStatus = ProvisioningStatus.Enabled; |
| 83 | + ``` |
| 84 | + |
| 85 | + * Replace the `ProvisioningConnectionString` placeholder value with the connection string of the provisioning service that you want to create the enrollment for. |
| 86 | + |
| 87 | + * You may optionally change the registration ID, endorsement key, device ID, and provisioning status. |
| 88 | + |
| 89 | + * If you're using this quickstart together with the [Create and provision a simulated TPM device using C# device SDK](quick-create-simulated-device-tpm-csharp.md) quickstart to provision a simulated device, replace the endorsement key and registration ID with the values that you noted down in that quickstart. You can replace the device ID with the value suggested in that quickstart, use your own value, or use the default value in this sample. |
| 90 | + |
| 91 | +1. Add the following method to the `Program` class. This code creates individual enrollment entry and then calls the `CreateOrUpdateIndividualEnrollmentAsync` method on the `ProvisioningServiceClient` to add the individual enrollment to the provisioning service. |
| 92 | + |
| 93 | + ```csharp |
| 94 | + public static async Task RunSample() |
| 95 | + { |
| 96 | + Console.WriteLine("Starting sample..."); |
| 97 | + |
| 98 | + using (ProvisioningServiceClient provisioningServiceClient = |
| 99 | + ProvisioningServiceClient.CreateFromConnectionString(ProvisioningConnectionString)) |
| 100 | + { |
| 101 | + #region Create a new individualEnrollment config |
| 102 | + Console.WriteLine("\nCreating a new individualEnrollment..."); |
| 103 | + Attestation attestation = new TpmAttestation(TpmEndorsementKey); |
| 104 | + IndividualEnrollment individualEnrollment = |
| 105 | + new IndividualEnrollment( |
| 106 | + RegistrationId, |
| 107 | + attestation); |
| 108 | + |
| 109 | + // The following parameters are optional. Remove them if you don't need them. |
| 110 | + individualEnrollment.DeviceId = OptionalDeviceId; |
| 111 | + individualEnrollment.ProvisioningStatus = OptionalProvisioningStatus; |
| 112 | + #endregion |
| 113 | + |
| 114 | + #region Create the individualEnrollment |
| 115 | + Console.WriteLine("\nAdding new individualEnrollment..."); |
| 116 | + IndividualEnrollment individualEnrollmentResult = |
| 117 | + await provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollment).ConfigureAwait(false); |
| 118 | + Console.WriteLine("\nIndividualEnrollment created with success."); |
| 119 | + Console.WriteLine(individualEnrollmentResult); |
| 120 | + #endregion |
| 121 | + |
| 122 | + } |
| 123 | + } |
| 124 | + ``` |
| 125 | + |
| 126 | +1. Finally, replace the body of the `Main` method with the following lines: |
| 127 | + |
| 128 | + ```csharp |
| 129 | + RunSample().GetAwaiter().GetResult(); |
| 130 | + Console.WriteLine("\nHit <Enter> to exit ..."); |
| 131 | + Console.ReadLine(); |
| 132 | + ``` |
| 133 | + |
| 134 | +1. Build the solution. |
| 135 | + |
| 136 | +## Run the individual enrollment sample |
| 137 | + |
| 138 | +Run the sample in Visual Studio to create the individual enrollment for your TPM device. |
| 139 | + |
| 140 | +On successful creation, the Command Prompt window displays the properties of the new individual enrollment. |
| 141 | + |
| 142 | +You can verify that the individual enrollment has been created. Go to the Device Provisioning Service summary, and select **Manage enrollments**, then select **Individual Enrollments**. You should see a new enrollment entry that corresponds to the registration ID you used in the sample. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +Select the entry to verify the endorsement key and other properties for the entry. |
| 147 | + |
| 148 | +If you've been following the steps in the [Create and provision a simulated TPM device using C# device SDK](quick-create-simulated-device-tpm-csharp.md) quickstart, you can continue with the remaining steps in that quickstart to enroll your simulated device. Be sure to skip the steps to create an individual enrollment using the Azure portal. |
| 149 | + |
| 150 | +## Clean up resources |
| 151 | + |
| 152 | +If you plan to explore the C# service sample, don't clean up the resources created in this quickstart. Otherwise, use the following steps to delete all resources created by this quickstart. |
| 153 | + |
| 154 | +1. Close the C# sample output window on your computer. |
| 155 | + |
| 156 | +1. Navigate to your Device Provisioning service in the Azure portal, select **Manage enrollments**, and then select the **Individual Enrollments** tab. Select the *Registration ID* for the enrollment entry you created using this quickstart, and select **Delete**. |
| 157 | + |
| 158 | +1. If you followed the steps in [Create and provision a simulated TPM device using C# device SDK](quick-create-simulated-device-tpm-csharp.md) to create a simulated TPM device, do the following steps: |
| 159 | + |
| 160 | + 1. Close the TPM simulator window and the sample output window for the simulated device. |
| 161 | + |
| 162 | + 1. In the Azure portal, navigate to the IoT Hub where your device was provisioned. In the menu under **Explorers**, select **IoT Devices**, select the check box next to your device, and then select **Delete**. |
| 163 | + |
| 164 | +## Next steps |
| 165 | + |
| 166 | +In this quickstart, you’ve programmatically created an individual enrollment entry for a TPM device. Optionally, you created a TPM simulated device on your computer and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service. To learn about device provisioning in depth, continue to the tutorial for the Device Provisioning Service setup in the Azure portal. |
| 167 | + |
| 168 | +> [!div class="nextstepaction"] |
| 169 | +> [Azure IoT Hub Device Provisioning Service tutorials](./tutorial-set-up-cloud.md) |
0 commit comments