An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
Good question, this is something that can be a bit confusing when working with SAS authentication in IoT Hub.
Based on your scenario, your username format is actually correct, but the issue usually comes from how the SAS token is generated and scoped, not just the username itself.
What the correct username should look like
For MQTT device connection, the expected username format is
{your-iot-hub-hostname}/{device-id}/?api-version=2021-04-12
Example contoso.azure-devices.net/MyDevice01/?api-version=2021-04-12
Also
- client id should be the device id
- password should be the SAS token
Key point that often causes failure
Even if the username is correct, authentication will fail if the SAS token is generated incorrectly or from the wrong scope
This is the most common issue in this scenario.
IoT Hub supports two types of SAS tokens
1 device scoped SAS token 2 IoT Hub level SAS token using shared access policy
For device connections using MQTT
you must use device scoped SAS tokens
If you generate the SAS token using a shared access policy like iothubowner
- it may not work for device level MQTT authentication
- even though username format is correct
This is because IoT Hub expects the token to match the device identity stored in the identity registry [learn.microsoft.com]
What to verify step by step
Here are the main things to check:
First, confirm SAS token scope
- make sure the token is generated using device id
- resource URI should include devices/device-id
If this is missing, authentication will fail
Second, check resource URI inside SAS token
It should follow this pattern
your hub hostname followed by devices and device id
If resource URI is incorrect, signature validation fails
Third, check token expiry
- SAS tokens are time based
- expired tokens will silently fail authentication
Try generating a fresh token and test again
Fourth, confirm device identity exists in IoT Hub
- device id must be registered in IoT Hub
- SAS token must match that device
IoT Hub validates identity using stored symmetric keys [learn.microsoft.com]
Fifth, check protocol requirements
For MQTT
- port should be 8883
- TLS must be enabled
- correct API version should be included in username
Incorrect protocol setup can also cause connection failure
You asked whether a SAS token generated from shared access policy can be used directly
- for device MQTT connection, no
- you should use device scoped SAS token or device connection string
This is the reason your connection is failing even with correct username.
In short
- your username format looks correct
- main issue is likely SAS token scope
- use device scoped SAS token instead of hub level policy
- verify resource URI and expiry
- ensure device exists in IoT Hub
I hope this helps. Do let me know if you have any further queries.
If this answers your query, please do click Accept Answer and Yes for was this answer helpful.
Thankyou!