Azure Custom Vision:使用托管标识或其他 Azure 标识


🤖本文由 AI(大语言模型)翻译。可能存在错误或不准确之处。如需查看原文,请参阅原文。
🌐本文还有其他语言版本:源站 / English

参考:Custom Vision: Azure role-based access control、Custom Vision: azure.identity credentials aren’t supported

C# 代码

首先,是 C# 代码。你也可以将其翻译成其他语言,比如 Python。

using System;
using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.CognitiveServices.Vision.CustomVision.Training;
using Microsoft.Rest;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string customVisionEndpoint = "https://<your_project_name>.cognitiveservices.azure.com/";

            try
            {
                // If you are using a managed identity, you can use the following code to get the token
                //var miClientId = "<your_managed_identity_client_id>";
                //var tokenCredential = new ManagedIdentityCredential(miClientId);

                // If you are using DefaultAzureCredential for local development, you can use the following code to get the token
                var tokenCredential = new DefaultAzureCredential();

                var mercuryResourceUri = "https://cognitiveservices.azure.com";
                var tokenRequestContext = new TokenRequestContext(new[] { $"{mercuryResourceUri}/.default" });
                var token = tokenCredential.GetToken(tokenRequestContext).Token;

                // Create a CustomVisionTrainingClient
                var trainingClient = new CustomVisionTrainingClient(new TokenCredentials(token))
                {
                    Endpoint = customVisionEndpoint,
                };

                Console.WriteLine("CustomVisionTrainingClient Instance Created");

                var projects = trainingClient.GetProjects();
                foreach (var project in projects)
                {
                    Console.WriteLine($"Project Name: {project.Name}, Project ID: {project.Id}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"exception: {ex.Message}");
            }
        }
    }
}

但是,它为什么能工作?

  • 使用“托管标识”或其他标识“登录”到 Resource Manager 终结点 (cognitiveservices.azure.com)
  • 获取令牌字符串
  • 将该令牌用作密钥来访问 CustomVisionTrainingClient / CustomVisionPredictionClient。
  • 做任何你想做的事!

在 Azure 门户中设置

按照 Custom Vision: Azure role-based access control,为你的托管标识或像你这样的用户添加角色。