Rohi_Dev_1.0
Rohi_Dev_1.0

Reputation: 386

create azure IOT hub device using .Net

I want to create IOT hub for a client. I have created IOT Hub using rest API but not able to find the way to create device in it. I am using Rest API to create IOT hub. It gets created But when I am trying to create device, I got unauthorized exception. I am using this api.

Upvotes: 2

Views: 2015

Answers (2)

Ian Mercer
Ian Mercer

Reputation: 39277

Use RegistryManager from the SDK Microsoft.Azure.Devices and a connection string from the Shared Access Policies tab in the Azure Portal with registry write:

var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var device = new Microsoft.Azure.Devices.Device(name);
var deviceWithKeys = await registryManager.AddDeviceAsync(device);

Upvotes: 3

Rita Han
Rita Han

Reputation: 9710

You need use IoT Hub SAS token to create device like this:

PUT /devices/device2?api-version=2016-11-14 HTTP/1.1
Host: [IOT-HUB-NAME].azure-devices.net
Authorization: SharedAccessSignature sr=[IOT-HUB-NAME].azure-devices.net&sig=[SIG]&se=1557553675&skn=iothubowner
Content-Type: application/json

{deviceId:"device2"}

You can get IoT Hub SAS token from device explorer.

Upvotes: 1

Related Questions