How to authenticate REST APIs for Azure Resource Manager
How to authenticate REST APIs for Azure Resource Manager
In this post we will try to understand how to get authenticated for Azure Resource Manager Rest APIs. As we understand that all of the tasks that we do on resources using the Azure Resource Manager (ARM) must be authenticated with Azure Active Directory.
Following are the steps one need to follow in order to authenticate against Azure Active Directory:
Add an application to Azure Active Directory tenant
- Sign in to the Azure portal.
- Using the subscription that contains your service instance for which we need the authentication token. Navigate to the App registrations tab in Azure Active Directory (AAD) (Azure Active Directory >> Manage/App registrations).
- Click on new application registration.
- The create window appears on the right. That's where we need to enter the AAD app relevant information.
- Enter a name for the application.
- For the application type, select Native.
- Enter a placeholder URL such as http://resources for the Redirect URI, as it's a required field, but the value isn't used later for the purpose of authentication or so, hence it can be any valid URL structure. Click the check box to save the application.
- Click Create.
Through this step we have added a placeholder for our application/ service to be recognized by Azure Active Directory. Now we need to provide permissions to this application.
Set permission for the application that we added
- Once the application is created, click API permissions.
- Click + Add a permission.
- Press Select Microsoft APIs.
- Choose Azure Service Management.
- Press Select.
- Click Delegated Permissions beside the newly added application, check the box for Access Azure Service Management (preview).
- Press Select.
- Click Grant Permissions.
Now that we have provided the required permission for the newly added application in Azure Active Directory, we can start requesting for the authentication token.
Get token for authenticating requests to Azure Resource Manager
Following is a small code snippet to get an authentication token to call Azure Resource Manager APIs. Once the above-mentioned steps are completed.
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
namespace GetTokenResourceManagerRequests
{
class Program
{
static void Main(string[] args)
{
var authenticationContext = new AuthenticationContext("https://login.microsoftonline.com/{tenant id}");
var result = authenticationContext.AcquireTokenAsync("https://management.azure.com/", "{application id}", new Uri("{redirect uri}"), new PlatformParameters(PromptBehavior.Auto)).Result;
if (result == null) {
throw new InvalidOperationException("Failed to obtain the JWT token");
}
Console.WriteLine(result.AccessToken);
Console.ReadLine();
}
}
}
Where we will have to replace the following:
{tenant id} - Tenant ID of the Azure Active Directory application we created. You can access the ID by clicking App registrations -> Endpoints.
{application id} - with the value we get by navigating to the Settings page.
{redirect uri} - value from the Redirect URIs tab of the Azure Active Directory application.
Hope you liked the post, do provide inputs in the comment, also if you think the article will be helpful for any of your friends and relatives then please do share the article with them.
And don't forget to subscribe for the new post available so that you get notified on your mailbox.
Happy Learning. 📚
Comments