IoT Hub Device Provisioning Service - Device Registration Status Lookup API C# Code Sample Required

OJB1 31 Reputation points
2022-08-17T17:38:15.04+00:00

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:

232118-image.png

232163-image.png

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,209 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
224 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Dom 1,631 Reputation points Microsoft Employee
    2022-08-24T13:24:49.267+00:00

    Can you get the device registration information you need by using: https://learn.microsoft.com/dotnet/api/microsoft.azure.devices.provisioning.service.provisioningserviceclient.getdeviceregistrationstateasync?view=azure-dotnet ?

    using var provisioningServiceClient = ProvisioningServiceClient.CreateFromConnectionString(<your DPS service connection string>);  
    var result = await provisioningServiceClient.GetDeviceRegistrationStateAsync("deviceid");  
    Console.WriteLine(result.Status);
    

  2. AshokPeddakotla-MSFT 35,096 Reputation points
    2022-09-05T04:40:57.517+00:00

    @OJB1 If you are still blocked,

    Adding to Dominic answer, I believe the link to this line of the IoT SDK for .NET should answer your question regarding Device Registration Status Lookup API: https://github.com/Azure/azure-iot-sdk-csharp/blob/076d689c2b275838023fc78da50a70ccefd16a65/provisioning/transport/http/src/Generated/RuntimeRegistration.cs#L274

    Do let us know if that helps or do you have any further queries.

    If the response is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.