Azure 从连接的设备/DPS 服务获取 IoTHub 连接字符串

问题描述

我正在使用 Azure 设备预配服务 (DPS) 自动注册和预配设备到 Azure IoT 中心。

我想获取特定设备的设备孪生。

我可以使用 de DeviceClient 类设法获取设备孪生:

var result = await provisioningDeviceClient.Registerasync().ConfigureAwait(false);
DeviceAuthenticationWithX509Certificate deviceAuthentication = new DeviceAuthenticationWithX509Certificate(result.deviceid,certificate);
var deviceClient = DeviceClient.Create(result.AssignedHub,deviceAuthentication,TransportType.Mqtt_Tcp_Only);
var deviceTwin = await deviceClient.GetTwinAsync().ConfigureAwait(false);

但我想使用 RegistryManager 获取设备,这需要特定 IoTHub 的连接字符串。

using (var registryManager = RegistryManager.CreateFromConnectionString(IotHubConnectionString))
{
    var deviceTwin = await registryManager.GetTwinAsync(result.deviceid);
}

是否可以从 DPS、连接的设备或 IoT 中心名称(即 result.AssignedHub)获取 IoT 中心连接字符串?我使用的是 c# SDK。 >

我已经看到了一种可能的方法https://github.com/MicrosoftDocs/azure-docs.es-es/blob/master/articles/iot-hub/iot-hub-devguide-security.md#compatibilidad-con-c 但是,我无法看到他们在示例中使用的变量 deviceGatewayConnectionString 来自哪里。

解决方法

_deviceClient = DeviceClient.Create(IotHubUri,new 
 DeviceAuthenticationWithRegistrySymmetricKey(DeviceId,DeviceKey),TransportType.Mqtt);

为“DeviceKey”输入符号

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.devices.client.deviceauthenticationwithregistrysymmetrickey?view=azure-dotnet

我想这可能对你有帮助:)