英文:
How to use User managed identity to access Azure BatchClient programatically?
问题
我们通常可以在SecretClient
构造函数中使用DefaultAzureCredential
对象,以使用托管身份验证来访问KeyVault。
我在寻找类似的东西,可以使用DefaultAzureCredential或其他机制来实例化BatchClient
的实例,而不必提供BatchAccountKey。
英文:
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.
答案1
得分: 0
以下是您提供的内容的翻译:
我正在寻找类似的东西,可以使用DefaultAzureCredential或其他一些机制来实例化
BatchClient
,而无需提供BatchAccountKey。
您可以在Azure批处理中使用Defaultazurecredential和C#来使用用户分配的托管标识。
为了解决这个问题,您可以使用下面的代码来列出使用用户分配的托管标识的Azure批处理帐户中的池。
在我的批处理帐户中,有两个池:
代码:
using Azure.Identity;
using Microsoft.Azure.Batch;
using System;
using System.Threading.Tasks;
namespace Tolistpools
{
class Program
{
static async Task Main(string[] args)
{
// 用您的Batch帐户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))
{
// 列出帐户中的池
var poolList = await client.PoolOperations.ListPools().ToListAsync();
foreach (var pool in poolList)
{
Console.WriteLine(pool.Id);
Console.WriteLine(pool.State);
}
}
}
}
}
输出:
123
活动
456
活动
希望这对您有帮助!
<details>
<summary>英文:</summary>
> 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:
![enter image description here](https://i.imgur.com/TFO1uI5.png)
**Code:**
```csharp
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论