Creating an IoT Device with SAS Token Authentication

This page describes how to set up an IoT Device in Azure IoT Hub using the SAS Token authentication method.

There can be only one connection between a specific device and the Azure IoT Hub. If a new device with the same Device ID is connected from somewhere else, all previous connections will be abandoned.

Requirements

  • Azure account with a valid subscription and all the necessary permissions to add and manage resources. There also should be at least one Resource Group. More information about Resource Groups is available on the official Microsoft Documentation webpage.

  • Access to an IoT Hub. The configuration of a sample IoT Hub for testing purposes is described here.

Configuration of the Azure IoT Device

1 Go to the Azure Portal and select the IoT Hub resource. You can find it in   All Resources  .

2 Copy the contents of IoT Hub  Settings  Shared access policies  iothubowner  Connection string-primary key to the clipboard. This will be referred to as the Connection String later.
It should look similar to this:

HostName=inmationMqttTest.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=cHJpbWFyeSBvciBzZWNvbmRhcnkga2V5Ow==

3 In VS Code, open the Explorer tab and go to the AZURE IOT HUB section.

4 In the Devices subsection, click on → Set IoT Hub Connection String and paste the Connection String from the clipboard into the input box.

5 In the Azure Portal, select the IoT Hub resource (The one from Preparing Azure Infrastructure ).

6 Click IoT Hub  IoT devices  + New to create a new device.

7 Enter a Device ID ('indevice' in this example) and keep a note of it, as it will be needed when configuring the Cloud Sink object, where it will be referred to as Device ID.

8 Set Authentication type to Symmetric key and leave everything else unchanged. Then click   Create  .

9 Return to VS Code and refresh the Device section. The newly created device should appear on the list.

10 Right-click the new device and select the Generate SAS Token for Device option.

11 Insert an expiration time in hours in the input box, for example 24 hours.

The generated SAS Token will be automatically placed in the clipboard as well as written to the output console.
It should look similar to this:

SharedAccessSignaturesr=inmationMqttTest.azure-devices.net%2Fdevices%2Findevice&sig=X%2B3r3iqIOu%2B9jPkmcy7pJ496RiPL8scvplo9A%2F18g2k%3D&se=1572964996

Like the Device ID, the SAS Token is needed for the configuration of the Cloud Sink object.

When the SAS Token expires, it will no longer be accepted by Azure MQTT Broker and a new one needs to be generated. It is possible to specify a longer period of time when generating a token, but it is highly recommended to use X.509 Authentication instead.