在Azure IoT后端中,如何使用Microsoft.Azure.Devices.RegistryManager(或其他类(如果需要的话)来更改设备的主键?
以下行将回收设备对称密钥:
var device = registryManager.UpdateDeviceAsync(new Device(deviceId), true).Result;
仅更改设备主键的情况:
var device = registryManager.GetDeviceAsync(deviceId).Result;
var auth = new AuthenticationMechanism();
auth.SymmetricKey.PrimaryKey = "<new primary key>";
auth.SymmetricKey.SecondaryKey = device.Authentication.SymmetricKey.SecondaryKey;
device = registryManager.UpdateDeviceAsync(new Device(deviceId) { Authentication = auth}, true).Result;
或者您可以简化它:
var device = registryManager.GetDeviceAsync(deviceId).Result;
device.Authentication.SymmetricKey.PrimaryKey = "<new primary key>";
device = registryManager.UpdateDeviceAsync(device, true).Result;