0

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);
        }
    }
}
Duncan Groenewald
  • 8,496
  • 6
  • 41
  • 76
  • have a look at the doc https://learn.microsoft.com/en-us/rest/api/iotcentral/ and REST APIs references – Roman Kiss Aug 25 '23 at 11:49
  • Do you want to be more specific ? – Duncan Groenewald Aug 25 '23 at 11:55
  • The Azure IoT Central REST APIs can be authorized using the header with either an API token or an AAD bearer token. More details can be found in the https://learn.microsoft.com/en-us/rest/api/iotcentral/authentication – Roman Kiss Aug 25 '23 at 12:01

2 Answers2

0

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
   }
  ]
}
Roman Kiss
  • 7,925
  • 1
  • 8
  • 21
  • I think you misunderstood my question - I am looking for the C# APIs for authenticating the user before they can access the IOT Central APIs as per the link in my original post. This is a WPF/C# application hence the request for the C# APIs. – Duncan Groenewald Aug 25 '23 at 12:37
  • Sorry, you are right. – Roman Kiss Aug 25 '23 at 12:50
0

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();
Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
  • Thanks, I will try these. I need the user to use dual factor authentication - they are already registered in the Azure account but they need to use DFA to use the application because they will be remotely configuring devices and installing the device credentials. So no need to go through the sign up and sign in process. – Duncan Groenewald Aug 25 '23 at 12:58
  • Is any of this compatible with .Net Framework 4.8.1 ? For a variety of other reasons we can't use .net6. – Duncan Groenewald Aug 25 '23 at 13:11
  • Sadly this ends up here - same error I get when running the Example example. System.UriFormatException: 'Invalid URI: The hostname could not be parsed.' – Duncan Groenewald Aug 25 '23 at 13:20
  • OK, thanks it seems the sample Constants file puts brackets around the appId, Tenant and subdomain when these should not be present. Hence the invalid Uri !!Boyt the MS examples and docs leave much to be desired. – Duncan Groenewald Aug 25 '23 at 13:44
  • Now what are the options for not using the Browser ? Any suggestions on where to look for examples on how to create a custom login with WPF controls ? A browser won't be available on the devices ! I will post another question but MS AD auth docs are - well where would you start. – Duncan Groenewald Aug 25 '23 at 13:50
  • Sorry, I never used WPF, so I won't be of use there. The WPF samples that I did see all go the 'browser route'. Hope you have better luck. – Matthijs van der Veer Aug 25 '23 at 14:28
  • 1
    OK I found the answer for device code here https://learn.microsoft.com/en-au/azure/active-directory/develop/scenario-desktop-acquire-token-device-code-flow?tabs=dotnet. – Duncan Groenewald Aug 25 '23 at 23:54