快速入门:使用适用于 IoT 中心设备预配服务的 C# 设备 SDK 创建和预配模拟的 TPM 设备Quickstart: Create and provision a simulated TPM device using C# device SDK for IoT Hub Device Provisioning Service

这些步骤展示如何使用用于 C# 的 Azure IoT 示例在运行 Windows OS 的开发计算机上模拟运行 TPM 设备。These steps show you how to use the Azure IoT Samples for C# to simulate a TPM device on a development machine running the Windows OS. 该示例还使用设备预配服务将模拟设备连接到 IoT 中心。The sample also connects the simulated device to an IoT Hub using the Device Provisioning Service.

示例代码使用 Windows TPM 模拟器作为设备的硬件安全模块 (HSM)The sample code uses the Windows TPM simulator as the Hardware Security Module (HSM) of the device.

如果不熟悉自动预配过程,还务必查看自动预配概念If you're unfamiliar with the process of autoprovisioning, be sure to also review Auto-provisioning concepts. 另外,在继续操作之前,请确保已完成通过 Azure 门户设置 IoT 中心设备预配服务中的步骤。Also make sure you've completed the steps in Set up IoT Hub Device Provisioning Service with the Azure portal before continuing.

Azure IoT 设备预配服务支持两类注册:The Azure IoT Device Provisioning Service supports two types of enrollments:

本文将演示单个注册。This article will demonstrate individual enrollments.

备注

初始的设备孪生状态配置仅在 IoT 中心的标准层中提供。The initial device twin state configuration is available only in the standard tier of IoT Hub. 有关基本和标准 IoT 中心层的详细信息,请参阅如何选择合适的 IoT 中心层For more information about the basic and standard IoT Hub tiers, see How to choose the right IoT Hub tier.

准备开发环境Prepare the development environment

  1. 确保在计算机上已安装 .NET Core 2.1 SDK 或更高版本Make sure you have the .NET Core 2.1 SDK or later installed on your machine.

  2. 确保在计算机上安装 git 并将其添加到可供命令窗口访问的环境变量。Make sure git is installed on your machine and is added to the environment variables accessible to the command window. 请参阅软件自由保护组织提供的 Git 客户端工具,了解要安装的最新版 git 工具,其中包括 Git Bash,这是一个命令行应用,可以用来与本地 Git 存储库交互。See Software Freedom Conservancy's Git client tools for the latest version of git tools to install, which includes the Git Bash, the command-line app that you can use to interact with your local Git repository.

  3. 打开命令提示符或 Git Bash。Open a command prompt or Git Bash. 克隆用于 C# GitHub 存储库的 Azure IoT 示例:Clone the Azure IoT Samples for C# GitHub repo:

    git clone https://github.com/Azure-Samples/azure-iot-samples-csharp.git
    

预配模拟的设备Provision the simulated device

  1. 登录到 Azure 门户。Sign in to the Azure portal. 选择左侧菜单上的“所有资源”按钮,打开设备预配服务 。Select the All resources button on the left-hand menu and open your Device Provisioning service. 在“概览”边栏选项卡中,记下“ID 范围”的值。 From the Overview blade, note the ID Scope value.

    从门户边栏选项卡复制预配服务范围 ID

  2. 在命令提示符处将目录更改为 TPM 设备预配示例的项目目录。In a command prompt, change directories to the project directory for the TPM device provisioning sample.

    cd .\azure-iot-samples-csharp\provisioning\Samples\device\TpmSample
    
  3. 键入以下命令,生成并运行 TPM 设备预配示例。Type the following command to build and run the TPM device provisioning sample. <IDScope> 值替换为预配服务的 ID 范围。Replace the <IDScope> value with the ID Scope for your provisioning service.

    dotnet run <IDScope>
    

    此命令将在单独的命令提示中启动 TPM 芯片模拟器。This command will launch the TPM chip simulator in a separate command prompt. 在 Windows 上,你可能会遇到 Windows 安全警报,该警报询问你是否要允许 Simulator.exe 在公共网络上通信。On Windows, you may encounter a Windows Security Alert that asks whether you want to allow Simulator.exe to communicate on public networks. 对于本示例,可以取消该请求。For the purposes of this sample, you may cancel the request.

  4. 原始命令窗口显示进行设备注册所需的“认可密钥”、“注册 ID”以及建议的“设备 ID”。 The original command window displays the Endorsement key, the Registration ID, and a suggested Device ID needed for device enrollment. 记下这些值。Take note of these values. 这些值将用于在设备预配服务实例中创建个人注册。You will use these value to create an individual enrollment in your Device Provisioning Service instance.

    备注

    请勿混淆包含命令输出的窗口与包含 TPM 模拟器输出的窗口。Do not confuse the window that contains command output with the window that contains output from the TPM simulator. 可能需要选择原始命令窗口,将其置于前台。You may have to select the original command window to bring it to the foreground.

    命令窗口输出

  5. 在 Azure 门户中的“设备预配服务”菜单中,选择“管理注册” 。In the Azure portal, from the Device Provisioning Service menu, select Manage enrollments. 选择“个人注册”选项卡,然后选择顶部的“添加个人注册”按钮。 Select the Individual Enrollments tab and select the Add individual enrollment button at the top.

  6. 在“添加注册”面板中,输入以下信息 :In the Add Enrollment panel, enter the following information:

    • 选择“TPM” 作为标识证明机制Select TPM as the identity attestation Mechanism.

    • 使用你之前记下的值输入 TPM 设备的“注册 ID”和“认可密钥” 。Enter the Registration ID and Endorsement key for your TPM device from the values you noted previously.

    • 选择与预配服务链接的 IoT 中心。Select an IoT hub linked with your provisioning service.

    • (可选)可以提供以下信息:Optionally, you may provide the following information:

      • 输入一个唯一“设备 ID” (可以使用建议的设备 ID,也可以提供自己的设备 ID)。Enter a unique Device ID (you can use the suggested one or provide your own). 为设备命名时,请确保避免使用敏感数据。Make sure to avoid sensitive data while naming your device. 如果选择不提供此项,则系统将改用注册 ID 来标识设备。If you choose not to provide one, the registration ID will be used to identify the device instead.
      • 使用设备所需的初始配置更新“初始设备孪生状态” 。Update the Initial device twin state with the desired initial configuration for the device.
    • 完成后,按“保存”按钮 。Once complete, press the Save button.

      在门户边栏选项卡中输入设备注册信息

    成功注册以后,设备的“注册 ID”会显示在“单个注册”选项卡下的列表中。 On successful enrollment, the Registration ID of your device will appear in the list under the Individual Enrollments tab.

  7. 在命令窗口(显示“认可密钥”、“注册 ID”和建议的“设备 ID”)中按 Enter,以登记模拟设备。 Press Enter in the command window (the one that displayed the Endorsement key, Registration ID, and suggested Device ID) to enroll the simulated device. 请注意相关消息,这些消息模拟设备启动后连接到设备预配服务以获取 IoT 中心信息的情况。Notice the messages that simulate the device booting and connecting to the Device Provisioning Service to get your IoT hub information.

  8. 验证设备是否已预配。Verify that the device has been provisioned. 将模拟设备成功预配到与预配服务链接的 IoT 中心以后,设备 ID 会显示在该中心的“IoT 设备”边栏选项卡上 。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.

    设备注册到 IoT 中心

    如果从设备的注册项中的默认值更改了“初始设备孪生状态” ,则它会从中心拉取所需的孪生状态,并执行相应的操作。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. 有关详细信息,请参阅了解并在 IoT 中心内使用设备孪生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.

  1. 关闭计算机上的设备客户端示例输出窗口。Close the device client sample output window on your machine.
  2. 关闭计算机上的 TPM 模拟器窗口。Close the TPM simulator window on your machine.
  3. 在 Azure 门户的左侧菜单中选择“所有资源”,然后选择设备预配服务 。From the left-hand menu in the Azure portal, select All resources and then select your Device Provisioning service. 在“概述”边栏选项卡顶部,按窗格顶部的“删除” 。At the top of the Overview blade, press Delete at the top of the pane.
  4. 在 Azure 门户的左侧菜单中选择“所有资源”,然后选择 IoT 中心 。From the left-hand menu in the Azure portal, select All resources and then select your IoT hub. 在“概述”边栏选项卡顶部,按窗格顶部的“删除” 。At the top of the Overview blade, press Delete at the top of the pane.

后续步骤Next steps

在本快速入门中,你已在计算机上创建 TPM 模拟设备,并已使用 IoT 中心设备预配服务将其预配到 IoT 中心。In this quickstart, you’ve created a TPM simulated device on your machine and provisioned it to your IoT hub using the IoT Hub Device Provisioning Service. 若要了解如何以编程方式注册 TPM 设备,请继续阅读快速入门中关于 TPM 设备的编程注册内容。To learn how to enroll your TPM device programmatically, continue to the quickstart for programmatic enrollment of a TPM device.