Hello,
Can anyone point me to a C# code sample in how to use the "Runtime Registration - Device Registration Status Lookup" API by implementing the code through C#?
The MS docs does not give any helpful information in how to use the service unless without doing it through an HTTP REST call using something like postman. https://learn.microsoft.com/en-us/rest/api/iot-dps/device/runtime-registration/device-registration-status-lookup
I'm expecting to find an example that uses the C# HTTP Client class.
My use case is based on completion of the IOT device provisioning service with TPM. https://learn.microsoft.com/en-us/azure/iot-dps/quick-create-simulated-device-tpm?pivots=programming-language-ansi-c
Once registered the first time, on the next reboot I want to check the registration status before re-provisioning unnecessarily each time. I was hoping that the Microsoft.Azure.Devices.Provisioning.Client SDK might provide a C# alternative to using the Status Lookup API, or at least provide a code sample somewhere in how to use the service through C#.
I've tried to implement a C# Http Client call using my code sample below, but I always get a StatusCode: 401, ReasonPhrase: 'Unauthorized' returned.
public class RequestBody
{
[JsonPropertyName("payload")]
public string? Payload { get; set; }
[JsonPropertyName("registrationId")]
public string? RegistrationId { get; set; }
[JsonPropertyName("tpm")]
public Tpm? Tpm { get; set; }
}
public class Tpm
{
[JsonPropertyName("endorsementKey")]
public string? EndorsementKey { get; set; }
[JsonPropertyName("storageRootKey")]
public string? StorageRootKey { get; set; }
}
private async Task CallWebApiWithPostRequest(string deviceId)
{
Console.WriteLine("Initializing security using the local TPM...");
//using SecurityProviderTpm security = new SecurityProviderTpmHsm("MyRegistraton12345678");
using SecurityProviderTpm security = new SecurityProviderTpmHsm(null); // Registration Id
Console.WriteLine("Creating TPM authentication for IoT Hub...");
IAuthenticationMethod auth = new DeviceAuthenticationWithTpm(deviceId, security);
var _idScope = "myScopeId";
var _registrationId = "MyRegistraton12345678";
//var _endorsementKey = Convert.ToBase64String(security.GetEndorsementKey());
//var _storageRootKey = Convert.ToBase64String(security.GetStorageRootKey());
var _endorsementKey = security.GetEndorsementKey().ToString();
var _storageRootKey = security.GetStorageRootKey().ToString();
string webApiUrl = $"https://global.azure-devices-provisioning.net/{_idScope}/registrations/{_registrationId}?api-version=2021-06-01";
var tpm = new Tpm
{
EndorsementKey = _endorsementKey,
StorageRootKey = _storageRootKey
};
var requestBody = new RequestBody
{
Payload = "",
RegistrationId = _registrationId,
Tpm = tpm
};
var requestBodySerialized = JsonSerializer.Serialize(requestBody);
using (var client = new HttpClient())
{
StringContent httpContent = new StringContent(requestBodySerialized, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(webApiUrl, httpContent);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("");
Console.WriteLine("API Call Failed!");
}
else
{
Console.WriteLine("");
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
When reviewing the API Docs, I believe I'm including the necessary properties in the request body: