使用 mqtt 以编程方式在 Azure IoT 中心注册设备

问题描述

无论如何,是否可以使用自己/CA 创建的证书以编程方式将新设备注册到 azure iot-hub 上?

例如,我想将我的 raspberry pi 从同一个 rasp pi 注册到我新创建的 IOT-HUB。我知道我们可以使用 AZ Cli 来做到这一点。我在寻找的是有没有一种方法可以使用 MQTT/REST 以编程方式完成?

提前致谢。

问候, 普拉迪普

解决方法

只是澄清一下,在 IoTHub/DPS 中“注册”设备是一个过度使用的术语,对不同的人可能意味着不同的事情。如果您使用的是自签名证书,则需要两个步骤。

首先,您需要enroll DPS 中的设备,这让 DPS 知道在未来的某个时候,设备可能会显示该名称和该证书。这通常是一个后端过程,通常不是从设备本身完成的。从技术上讲,没有什么可以阻止您从设备调用 REST API 来执行此操作,但由于您需要一些非常强大的凭据才能执行此操作,因此不建议将它们放在您的设备上。这不能通过 MQTT 完成。不过,您可以通过上传、验证和使用 CA 签署的证书来避免注册每台设备。

完成后,设备现在/稍后可以注册自己,这是实际让 DPS 在 IoT 中心创建设备注册记录的行为。设备“回拨”到 DPS,使用您在注册中提供的证书对其进行身份验证,然后进行自我注册,从 DPS 获取所需的 IoT 中心连接信息。该过程可以通过 MQTT 完成,您可以在我的博客上逐步找到该过程 -> http://busbyland.com/azure-device-provisioning-server-over-mqtt-using-x509-certificates

,

您可以使用设备配置服务 - DPS。 DPS 是另一项服务,其目的是识别您的设备,如果 DPS 识别出设备标识,DPS 将在您的 IoT 中心创建一个身份。

您设置 DPS 的方式是创建单独注册(针对单个设备的入职)或组注册(针对一组设备,通常如果您使用证书或共享访问密钥身份验证类型)。 注册通常包含设备应显示的标识类型,以及应将具有已显示标识的设备分配到的 IoT 中心。

典型的流程是设备使用一些公共标识(证书链、TPM 注册 ID 或 SAS 密钥)联系 DPS。然后在内部,DPS 可以质询设备(proof-of-possesion in case of CA certificates),如果设备成功解决了质询,则意味着该设备包含识别该设备的特定秘密(CA 证书中的私钥),因此DPS 将在该特定注册中为分配的集线器创建设备标识。此过程称为 attestation

因此,在设备端,您至少会收到 IoT 中心端点以及用于与 IoT 中心通信的设备 ID。

以下是有关如何使用 CA 证书和 C# 执行此操作的代码片段:

var certificate = new X509Certificate2(leafCertificatePath,leafCertificatePassword);
    using (var security = new SecurityProviderX509Certificate(certificate))
    using (var transport = new ProvisioningTransportHandlerMqtt(TransportFallbackType.TcpOnly))
    {
        ProvisioningDeviceClient provClient =
        ProvisioningDeviceClient.Create(GlobalDeviceEndpoint,dpsScope,security,transport);
    
        var deviceAuthentication = await GetDeviceRegistrationResultAsync(provClient,security);
    
        var auth = new DeviceAuthenticationWithX509Certificate(deviceAuthentication.DeviceId,security.GetAuthenticationCertificate());
    
        var deviceClient = DeviceClient.Create(hostname: deviceAuthentication.AssignedHub,authenticationMethod: auth,transportType: TransportType.Mqtt);
    
    
        if (deviceClient == null)
        {
            Console.WriteLine("Failed to create DeviceClient!");
        }
        else
        {
            SendEvents(deviceClient,MESSAGE_COUNT).Wait();
        }
    }

从DPS获取设备注册结果的方法:

static async Task<DeviceRegistrationResult> GetDeviceRegistrationResultAsync(ProvisioningDeviceClient provClient,SecurityProviderX509 security)
{
    DeviceRegistrationResult result = await provClient.RegisterAsync().ConfigureAwait(false);

    if (result.Status != ProvisioningRegistrationStatusType.Assigned)
    {
        throw new Exception("Device not assigned");
    }
    return result;
}

来自 MSFT 的官方示例,您可以找到 here

Here 介绍如何使用 IoT 中心和 DPS 创建和验证证书。

,

不建议从设备本身配置 IoTHub 中的设备,因为这将需要 IoTHub 注册表写入权限和服务 SDK 的使用。

推荐的方法是使用设备配置服务,您可以使用 X509 创建个人或组注册,它会自动在目标 IoTHub 中配置设备。

设备只需要

  1. 证明其身份的机制
  2. DPS 全局端点
  3. 用于标识您的 DPS 实例的 ID 范围。

检查这个 - https://docs.microsoft.com/en-us/azure/iot-dps/concepts-x509-attestation

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...