如何使用用户管理的身份来以编程方式访问Azure BatchClient?

huangapple go评论55阅读模式
英文:

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>

&gt; 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 = &quot;your-batch-account-url&quot;;

            var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { ManagedIdentityClientId = &quot;your-managed-identity-client-id&quot; });
            AccessToken token = await credential.GetTokenAsync(new Azure.Core.TokenRequestContext(new[] { &quot;https://batch.core.windows.net/&quot; }), 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

如何使用用户管理的身份来以编程方式访问Azure BatchClient?

huangapple
  • 本文由 发表于 2023年5月25日 03:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定