![](/img/trans.png)
[英]Bulk import register device on azure iot hub using c# not working?
[英]How to register a device using device type connection string in Azure IoT Hub c#?
在Azure IOT集線器中,我們有多種類型的連接字符串
要注冊設備,我可以使用SharedAccessKeyName=iothubowner
但要向設備發送消息,我需要使用SharedAccessKeyName=device
字符串,因為我將在運行時生成deviceId。
因此,如何使用SharedAccessKeyName=device
注冊設備,以便可以使用相同的連接字符串將消息發送到物聯網集線器設備。
注冊設備的代碼-
class Program
{
static RegistryManager registryManager;
static string connectionString = "HostName=mydemo.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=key!";
private static async Task AddDeviceAsync()
{
string deviceId = "dummydevice";
Device device;
try
{
device = await registryManager.AddDeviceAsync(new Device(deviceId));
}
catch (DeviceAlreadyExistsException)
{
device = await registryManager.GetDeviceAsync(deviceId);
}
Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);
}
static void Main(string[] args)
{
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
AddDeviceAsync().Wait();
Console.ReadLine();
}
對於要連接到IoT中心的設備,可以使用兩種類型的連接字符串:
您可以在中心的“共享訪問策略”頁面上的門戶網站中找到第一個。 您可以在設備的詳細信息頁面上的門戶網站中找到第二個。
文檔的以下部分說明了如何使用兩種不同類型的密鑰: https : //docs.microsoft.com/zh-cn/azure/iot-hub/iot-hub-devguide-security#use-sas-設備中的令牌
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.