An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
Hello Dharmesh Joshi,
Welcome to Microsoft Q&A and Thank you for reaching out,
In Azure IoT Hub / DPS The Primary Key you copy from the Azure portal is already Base64-encoded
So in most cases: You do NOT need to Base64 encode it again, You can use it directly as-is
Why this causes confusion
It’s technically possible to Base64 encode a string in .NET nanoFramework like this:
using System;
However, this is usually unnecessary and incorrect for IoT Hub, because:
- The portal already gives you a Base64 string
- Encoding it again leads to double encoding, which will cause authentication failures
Correct approach for IoT Hub / DPS
If you copied the key from Azure Portal
Just use it directly:
string primaryKey = "<YOUR_IOTHUB_PRIMARY_KEY>";
If you’re using nanoFramework DPS sample:
Pass it directly to:
-
DeviceAuthenticationWithRegistrySymmetricKey.KeyAsBase64String
When generating SAS tokens (important distinction)
Here’s the correct flow:
- Decode the Base64 key
byte[] keyBytes = Convert.FromBase64String(primaryKey);
- Generate HMAC-SHA256 signature
using (var hmac = new System.Security.Cryptography.HMACSHA256(keyBytes))
- Encode the signature to Base64
string signature = Convert.ToBase64String(signatureBytes);
- URL encode the result
string encodedSignature = System.Net.WebUtility.UrlEncode(signature);
Base64 encoding is applied to the signature, not the original key
When would you encode the key yourself?
Only if:
- You are starting with a raw (non-Base64) key, or
- You generated the key programmatically
This is not the case for standard IoT Hub keys from Azure
Azure IoT Hub primary keys are already Base64-encoded
Don’t encode them again (avoids double encoding issues)
Decode them when generating SAS tokens
Encode only the generated signature, not the original key
The primary key provided by Azure IoT Hub is already Base64-encoded, so you don’t need to encode it again in .NET nanoFramework.
While it is technically possible to Base64 encode a string using Convert.ToBase64String, doing so on the IoT Hub primary key will result in double encoding and cause authentication issues.
When using DPS (for example, with DeviceAuthenticationWithRegistrySymmetricKey), you can pass the key directly as the Base64 string.
If you are generating a SAS token, the correct approach is to first decode the primary key using Convert.FromBase64String, then use it to compute the HMAC-SHA256 signature, and finally Base64 encode the generated signature.
So in most scenarios, no additional Base64 encoding of the primary key is required.
Please refer this
Convert.ToBase64String: https://docs.microsoft.com/dotnet/api/system.convert.tobase64string
Encoding.UTF8.GetBytes: https://docs.microsoft.com/dotnet/api/system.text.encoding.utf8
DeviceAuthenticationWithRegistrySymmetricKey.KeyAsBase64String: https://learn.microsoft.com/dotnet/api/microsoft.azure.devices.client.deviceauthenticationwithregistrysymmetrickey.keyasbase64string
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.
Thank you!