Managing Azure Resources from C#

Is there any easy way manage Azure resources in C# or other .NET language? Yes, there is! The Azure SDK for .NET (Azure Client Libraries) gives you easy access to managing Azure. This article will explain how to use it!

Azure SDK for .NET

The Azure SDK for .NET provides programmatic access to your Azure Resources. Whether it is configuring an Azure Function, uploading files to a Data Lake or managing an Event Hub.

Everything is available as NuGet packages. There is a huge amount of packages available, which you can see from NuGet or in the list here. You can also find the source code of the SDK here on GitHub.

Azure SDK for .NET packages

Authentication

To be able to call the Azure SDK for .NET, your application needs to be authenticated and have the necessary access rights. The access is managed via Azure Active Directory and OAuth.

While you could authenticate as a user account, it is usually a much better idea to register an app in Azure AD:

Azure AD App Registrations

Register your application. You can choose any name and you can select Single tenant. Also you can leave the Redirect URI blank.

Azure AD New App Registration

Now you can add a client secret.

Azure AD Add Client Secret

Copy your secret value and save it for later!

Azure AD Copy Client Secret

Also copy and save your Client ID and Tenant ID for later.

Azure AD ClientID and TenantID

You also need to grant admin consent.

Azure AD Grand Admin Consent

Managing Azure access

You can now give your application access to any Azure resources. Go to Access control (IAM) on the resource, click Add role assignment and search for your application name. Your application should appear in the search results.

Azure App Access Control (IAM)

Installing the NuGet packages

Now add the NuGet packages you need. For example in Visual Studio:

Visual Studio - Manage NuGet Packages

Note that if you search for something like Azure.Cdn, you will get two options. One of them is the fluent library and the other one is the standard. The only difference between them is the syntax, so you can use any.

Azure SDK libraries

Writing the C# code

The first step of your application is to authenticate and acquire an access token.

1using Microsoft.Identity.Client;
2
3var scope = "https://management.core.windows.net//.default";
4var app = ConfidentialClientApplicationBuilder.Create(ClientId).WithClientSecret(ClientSecret).WithTenantId(TenantId).Build();
5var authResult = await app.AcquireTokenForClient(new[] { scope }).ExecuteAsync();

Use the ClientId, ClientSecret and TenantId that you received previously.

Now you can use the Azure SDK for .NET libraries. Here's an example of getting endpoints from the Cdn client (using the standard library, not the fluent):

1using Microsoft.Azure.Management.Cdn;
2
3var client = new CdnManagementClient(new TokenCredentials(authResult.AccessToken)) { SubscriptionId = YourSubscriptionId };
4var endpoint = client.Endpoints.Get(resourceGroupName, cdnName, cdnEndpointName);

You may also wish to enable logging. You can easily get logs written to the console:

1using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();

The documentation of the libraries is available here.

References

Related Posts