快速入门:使用 Node.js 服务 SDK 将 TPM 设备注册到 IoT 中心设备预配服务Quickstart: Enroll TPM device to IoT Hub Device Provisioning Service using Node.js service SDK

在本快速入门中,你将使用 Node.js 服务 SDK 和示例 Node.js 应用程序以编程方式在 Azure IoT 中心设备预配服务中为 TPM 设备创建单个注册。In this quickstart, you programmatically create an individual enrollment for a TPM device in the Azure IoT Hub Device Provisioning Service using the Node.js Service SDK and a sample Node.js application. 可以通过该单个注册项,选择性地将模拟的 TPM 设备注册到预配服务。You can optionally enroll a simulated TPM device to the provisioning service using this individual enrollment entry.

先决条件Prerequisites

创建单个注册示例Create the individual enrollment sample

  1. 在工作文件夹的命令窗口中,运行以下命令:From a command window in your working folder, run:

    npm install azure-iot-provisioning-service
    
  2. 使用文本编辑器,在工作文件夹中创建 create_individual_enrollment.js 文件。Using a text editor, create a create_individual_enrollment.js file in your working folder. 将以下代码添加到文件并进行保存:Add the following code to the file and save:

    'use strict';
    
    var provisioningServiceClient = require('azure-iot-provisioning-service').ProvisioningServiceClient;
    
    var serviceClient = provisioningServiceClient.fromConnectionString(process.argv[2]);
    var endorsementKey = process.argv[3];
    
    var enrollment = {
      registrationId: 'first',
      attestation: {
        type: 'tpm',
        tpm: {
          endorsementKey: endorsementKey
        }
      }
    };
    
    serviceClient.createOrUpdateIndividualEnrollment(enrollment, function(err, enrollmentResponse) {
      if (err) {
        console.log('error creating the individual enrollment: ' + err);
      } else {
        console.log("enrollment record returned: " + JSON.stringify(enrollmentResponse, null, 2));
      }
    });
    

运行单个注册示例Run the individual enrollment sample

  1. 若要运行示例,需要适用于预配服务的连接字符串。To run the sample, you need the connection string for your provisioning service.

    1. 登录到 Azure 门户,选择左侧菜单上的“所有资源”按钮,打开设备预配服务 。Sign in to the Azure portal, select the All resources button on the left-hand menu and open your Device Provisioning service.

    2. 选择“共享访问策略”,然后选择想要用来打开其属性的访问策略 。Select Shared access policies, then select the access policy you want to use to open its properties. 在“访问策略”窗口中,复制并记下主密钥连接字符串。 In the Access Policy window, copy and note down the primary key connection string.

      从门户获取预配服务连接字符串

  2. 此外还需适用于设备的许可密钥。You also need the endorsement key for your device. 如果已按照创建和预配模拟的设备快速入门教程创建模拟的 TPM 设备,请使用为该设备创建的密钥。If you have followed the Create and provision a simulated device quickstart to create a simulated TPM device, use the key created for that device. 否则,若要创建示例单个注册,可以使用 Node.js 服务 SDK 随附的以下许可密钥:Otherwise, to create a sample individual enrollment, you can use the following endorsement key supplied with the Node.js Service SDK:

    AToAAQALAAMAsgAgg3GXZ0SEs/gakMyNRqXXJP1S124GUgtk8qHaGzMUaaoABgCAAEMAEAgAAAAAAAEAxsj2gUScTk1UjuioeTlfGYZrrimExB+bScH75adUMRIi2UOMxG1kw4y+9RW/IVoMl4e620VxZad0ARX2gUqVjYO7KPVt3dyKhZS3dkcvfBisBhP1XH9B33VqHG9SHnbnQXdBUaCgKAfxome8UmBKfe+naTsE5fkvjb/do3/dD6l4sGBwFCnKRdln4XpM03zLpoHFao8zOwt8l/uP3qUIxmCYv9A7m69Ms+5/pCkTu/rK4mRDsfhZ0QLfbzVI6zQFOKF/rwsfBtFeWlWtcuJMKlXdD8TXWElTzgh7JS4qhFzreL0c1mI0GCj+Aws0usZh7dLIVPnlgZcBhgy1SSDQMQ==
    
  3. 若要为 TPM 设备创建单个注册,请运行以下命令(包括命令参数的引号):To create an individual enrollment for your TPM device, run the following command (include the quotes around the command arguments):

    node create_individual_enrollment.js "<the connection string for your provisioning service>" "<endorsement key>"
    
  4. 成功创建以后,命令窗口会显示新的单个注册的属性。On successful creation, the command window displays the properties of the new individual enrollment.

    命令输出中的注册属性

  5. 验证是否已创建单个注册。Verify that an individual enrollment has been created. 在 Azure 门户的设备预配服务摘要边栏选项卡上,选择“管理注册” 。In the Azure portal, on the Device Provisioning Service summary blade, select Manage enrollments. 选择“单个注册”选项卡,然后选择新的注册项 (first),验证认可密钥和该项的其他属性 。Select the Individual Enrollments tab and select the new enrollment entry (first) to verify the endorsement key and other properties for the entry.

    门户中的注册属性

为 TPM 设备创建单个注册以后,如果需要注册模拟的设备,则可继续执行创建和预配模拟的设备中的剩余步骤。Now that you've created an individual enrollment for a TPM device, if you want to enroll a simulated device, you can continue with the remaining steps in Create and provision a simulated device. 请确保跳过该快速入门中通过 Azure 门户创建单个注册的步骤。Be sure to skip the steps to create an individual enrollment using the Azure portal in that quickstart.

清理资源Clean up resources

如果打算学习 Node.js 服务示例,请勿清除本快速入门中创建的资源。If you plan to explore the Node.js service samples, 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. 关闭计算机上的 Node.js 示例输出窗口。Close the Node.js sample output window on your machine.
  2. 如果已创建模拟的 TPM 设备,请关闭 TPM 模拟器窗口。If you created a simulated TPM device, close the TPM simulator window.
  3. 在 Azure 门户中导航到你的设备预配服务,选择“管理注册”,然后选择“单个注册”选项卡。 选中通过本快速入门创建的注册项的“注册 ID”旁边的复选框,然后按窗格顶部的“删除”按钮 。Navigate to your Device Provisioning service in the Azure portal, select Manage enrollments, and then select the Individual Enrollments tab. Select the check box next to the Registration ID for the enrollment entry you created using this quickstart, and press the Delete button at the top of the pane.

后续步骤Next steps

本快速入门介绍了如何以编程方式为 TPM 设备创建单个注册项,以及如何选择性地在计算机上创建 TPM 模拟设备,并使用 Azure IoT 中心设备预配服务将其预配到 IoT 中心。In this quickstart, you’ve programmatically created an individual enrollment entry for a TPM device, and, optionally, created a TPM simulated device on your machine and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service. 若要深入了解设备预配,请继续学习本教程有关如何在 Azure 门户中进行设备预配服务设置的内容。To learn about device provisioning in depth, continue to the tutorial for the Device Provisioning Service setup in the Azure portal.