in azure iot hub we have multiple types of connection string
to register a device i can use SharedAccessKeyName=iothubowner
but to send a messages to device i need to use SharedAccessKeyName=device
coonection string as i'm going to generate deviceId at runtime.
So how can i register device using SharedAccessKeyName=device
so that I can use same connection string to send messages to iot hub device.
code to register 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();
}