Created
February 14, 2020 09:00
-
-
Save danigian/df730a472e474088b19713f4526ef0d4 to your computer and use it in GitHub Desktop.
IoT Hub Connection string retrieval with Managed Identities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static async Task GetIoTHubResource() | |
{ | |
var azureServiceTokenProvider = new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider(); | |
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/"); | |
var tokenCredentials = new Microsoft.Rest.TokenCredentials(accessToken); | |
var iotHubClient = new Microsoft.Azure.Management.IotHub.IotHubClient(tokenCredentials); | |
iotHubClient.SubscriptionId = subscriptionId; | |
var iotHubDescription = await iotHubClient.IotHubResource.GetAsync(resourceGroupName,iotHubName); | |
//You can replace iothubowner with the name of your Shared access policy | |
var keys = await iotHubClient.IotHubResource.GetKeysForKeyNameAsync(resourceGroupName,iotHubName,"iothubowner"); | |
Console.WriteLine($"Primary key for {iotHubDescription.Name} located in {iotHubDescription.Location}: {keys.PrimaryKey}"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp3.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Azure.Management.IotHub" Version="2.1.1" /> | |
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.4.0" /> | |
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="3.0.3" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment