We can typically use a DefaultAzureCredential
object in SecretClient
constructor to use managed identities to access a KeyVault.
I was looking for something similar where I can use the DefaultAzureCredential or some other mechanism through which I can instantiate an instance of BatchClient
without having to provide a BatchAccountKey.
I was looking for something similar where I can use the DefaultAzureCredential or some other mechanism to instantiate an instance of
BatchClient
without having to provide a BatchAccountKey.
You can use the Defaultazurecredential with user-assigned managed identity in Azure batch using C#.
Here for a workaround, You can use the below code to list the pools in the Azure batch account using a user-assigned managed identity.
In my batch account, I have two pools:
Code:
using Azure.Identity;
using Microsoft.Azure.Batch;
using System;
using System.Threading.Tasks;
namespace Tolistpools
{
class Program
{
static async Task Main(string[] args)
{
// Replace with your Batch account URL
string accountUrl = "your-batch-account-url";
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = "your-managed-identity-client-id" });
AccessToken token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { "https://batch.core.windows.net/" }), new System.Threading.CancellationToken());
BatchTokenCredentials cred = new BatchTokenCredentials(accountUrl, token.Token);
using (BatchClient client = BatchClient.Open(cred))
{
// List the pools in the account
var poolList = await client.PoolOperations.ListPools().ToListAsync();
foreach (var pool in poolList)
{
Console.WriteLine(pool.Id);
Console.WriteLine(pool.State);
}
}
}
}
}
Output:
123
Active
456
Active