I am trying to create a C# application to access Device IDs from IOTCentral. How can I authenticate the user of the application?

70 Views Asked by At

I am currently looking at these examples here but can't get the Example to work as it complains about some invalid format.

The Preview example works find but requires me to have logged in via the azure CLI. I need the users of the application to be able to log in to the application wherever it is to authenticate to IOTCentral.

What API is used to do that - the Azure examples seem to either not work or rely on the az CLI to handle the log in.

Error from Example System.ArgumentException: ''authority' should be in URI format. Arg_ParamName_Name'

PreviewExample

namespace PreviewExamples {
    internal class Program
    {        
        private static void Main(string[] args)
        {
            // Using 'az login' first, and using 'az account set -s subscription'
            var credential = new DefaultAzureCredential();

            var subdomain = "app-name";

            // Users example
            // UserRoleOrgsExample.Run(subdomain, credential);

            DeviceExample.Run(subdomain, credential);
        }
    }
}
2

There are 2 best solutions below

7
Matthijs van der Veer On BEST ANSWER

Quick C# sample where you have the user log in through a browser popup:

using var httpClient = new HttpClient();
var cred = new InteractiveBrowserCredential();
var token = await cred.GetTokenAsync(new TokenRequestContext(new[] {"https://apps.azureiotcentral.com/.default"}), CancellationToken.None);

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.Token);

using var centralClient = new AzureIoTCentral(httpClient, true);
centralClient.Subdomain = Constants.AppName;
centralClient.ApiVersion = Constants.ApiVersion;

var devices = await centralClient.Devices.ListAsync();

Or if you want to use an API key, like Roman suggested:

using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"SharedAccessSignature sr=stuffnotfortheinternet");

using var centralClient = new AzureIoTCentral(httpClient, true);
centralClient.Subdomain = Constants.AppName;
centralClient.ApiVersion = Constants.ApiVersion;

var devices = await centralClient.Devices.ListAsync();
2
Roman Kiss On

Based on my comments, the following is an example for obtaining all device IDs from IoT Central App using the Get request:

https://rk2022iotc-test.azureiotcentral.com/api/devices?api-version=2022-10-31-preview

Authorization: Api token

response:

{
  "value": [
    {
      "id": "device12",
      "displayName": "device12",
      "simulated": false,
      "provisioned": true,
      "etag": "eyJwZ0luc3*****kMDNhIn0=",
      "template": "dtmi:j9a***:x8**p8aly",
      "enabled": true
    },
    {
      "id": "device1234",
      "displayName": "device1234",
      "simulated": false,
      "provisioned": true,
      "etag": "eyJwZ0luc3*****kMDNhIn0=",
      "template": "dtmi:j9a***:x8**p8aly",
      "enabled": true
   }
  ]
}