Is managed identity available to create IoT Hub client to avoid Azure Active Directory app and secret

@IamCoder 391 Reputation points
2022-07-27T16:08:53.43+00:00

I am using below nuget package <PackageReference Include="Microsoft.Azure.Management.IotHub" Version="4.2.0" /> and trying to create IotHubClient.

I have registered one application with AAD and giving permission with IoT Hub instance and using below code to generate the IoT Hub client,

private async Task<IotHubClient> GetIotHubClient()  
    {  
        var authContext = new AuthenticationContext("https://login.microsoftonline.com/XXXXXXXXXXXX");  
        var credential = new ClientCredential(Configuration["AAD-APP-Client-ID", "AAD-APP-Client-Secret"));  
        var token = await authContext.AcquireTokenAsync("https://management.core.windows.net/", credential);  
  
        if (token == null) return null!;  
  
        var credentials = new TokenCredentials(token.AccessToken);  
        var client = new IotHubClient(credentials)  
        {  
            SubscriptionId = "my-SubscriptionId"  
        };  
  
        return client;  
    }  

It's works and I am able to create IoT hub client to talk with it.

Question is, is this the only way to prepare IoTHub client, can some means of managed identity available so that I don't need to main AAD app and their secret's ?

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

Accepted answer
  1. Sander van de Velde 28,311 Reputation points MVP
    2022-07-27T19:28:30.52+00:00

    Hello @@IamCoder ,

    In general, the solution you use is the right solution to get access to the IoT Hub for API functionality.

    See also this blog post and this blog post where a similar approach is used for the REST API:

    az login  
    az ad sp create-for-rbac -n "testaccount"  
    

    Normally this is provided by an administrator who has access to the production environment. So there is a boundary between programming and service management.

    As far as I know, it's not possible to skip it and using eg. the IoT Hub connection string (supporting the right policies).


0 additional answers

Sort by: Most helpful