No device connection string in IoT Hub

CogniTech 15 Reputation points
2024-02-07T20:34:39.3766667+00:00

We are building in the functionality to send cloud to device messages to trigger actions on device remotely. We are actually looking for how to create the device connection string programmatically, but when we check in IoT hub none of our devices have connection strings.

The following documentation states that they can be found on the device page in IoT hub resource in Azure, but they are not present. https://learn.microsoft.com/en-us/azure/iot-hub/c2d-messaging-dotnet#get-the-device-connection-string User's image

We actually need the information on how to recreate the connection string in PHP, but also would like to know why they do not exist in Azure?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,157 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sander van de Velde | MVP 31,211 Reputation points MVP
    2024-02-07T22:14:29.3066667+00:00

    Hello @Innov8,

    welcome to this moderated Azure community forum.

    This is weird, normally, the connection strings (primary/secundary) are shown next to the two keys.

    I just created this test device in the West Europe region (with autogenerated symmetric keys in the Azure portal):

    User's image

    How did you create this device? Using the Azure portal? Or using some script? If so, Can you share it?

    A connection string is not that special:

    HostName=[name of iot hub].azure-devices.net;DeviceId=[device name];SharedAccessKey=[one of the two keys]

    But... it is strongly recommended to make use the Device Provisioning Service to control future IoT Hub selection/migration. This way, you do not need to access all your device to migrate to another IoT Hub.

    Next to that, for Cloud to Device communication like a Direct Method call, you need to have the IoT Hub connectionstring!

    See also this example in the documentation.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


  2. Dom 1,631 Reputation points Microsoft Employee
    2024-02-08T08:20:02.89+00:00

    It looks like your device is configured to authenticate using an X.509 certificate rather than a shared access key - this is why your screenshot is showing thumbprints and not keys/connection-strings.

    The connection string for a device that uses X.509 authentication looks like:

    "HostName=<host_name>;DeviceId=<device_id>;x509=true"

    I notice as well that you're trying to send a cloud to device message - the device connection string is only for devices to connect to your hub. If you want to connect to your hub from a back-end service in order to send a cloud to device message, you need to use the IoT hub connection string - this is described further on in the article you were looking at here: https://learn.microsoft.com/azure/iot-hub/c2d-messaging-dotnet#get-the-iot-hub-connection-string. After your back-end service connects to IoT Hub, it just needs the device ID to send a C2D message.