This Azure SDK for .NET post will show how to connect and authenticate to Azure using the Armclient.
Armclient
The Armclient is a .NET class part of the Azure Resource Manager library and allows us to manage (list, create, update and list) resources on Microsoft Azure.
We need to follow the following steps to connect to Azure using the Armclient.
Step 1: Create App Registration
As shown on this blog many times, You must create an App Registration with enough permissions to Azure. Once you create an App Registration, copy the following details from it:
- Azure Tenant ID
- Azure Client ID
- Azure Client Secret
Step 2: Install Library
Create a .NET application and install the following package.
dotnet add package Azure.ResourceManager
dotnet add package Azure.Identity
Step 3: Create Environment Variables
Using the values from the App Registration step (2) create the following 3 environment variables.
$env:AZURE_TENANT_ID ="value"
$env:AZURE_CLIENT_ID ="value"
$env:AZURE_CLIENT_SECRET ="value"
Step 4: Use the Armclient
Finally, use the following code (C#) in your .NET application to connect to Azure and list the ID of the default Azure subscription.
using Azure.ResourceManager;
using Azure.Identity;
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Print the default Azure subscription using a method from the ArmClient class
var subdetails = armClient.GetDefaultSubscription();
Console.WriteLine ($"The default Azure subscription is: {subdetails.Id}");
Related Articles
- Connect to Azure Using Azure SDK for .NET
- How to Authenticate to Azure from Terraform
- How To Install .NET Core 2.0 SDK On Windows Server Core 2016 1709
- Create API Keys and Authenticate to OpenAI Using Python