使用托管标识连接到 Azure 应用程序配置时出现 403

403 when connecting to Azure App Configuration using a Managed Identity

我正在尝试使用托管标识从网络框架应用程序连接到 Azure 应用程序配置,但遇到权限问题。

我如何连接

options.Connect(new Uri("https://myconfigstore.azconfig.io"), new ManagedIdentityCredential(clientId));

我已经尝试了使用门户可以找到的所有各种 clientId、objectid 和 applicationId guid,但无论我何时使用 guid 调用它,总是收到错误请求

Azure.Identity.CredentialUnavailableException: 'ManagedIdentityCredential authentication unavailable, 
the requested identity has not been assigned to this resource.
Status: 400 (Bad Request)

如果我在没有指定 clientId 的情况下创建 ManagedIdentityCredential,我会收到此错误

Azure.RequestFailedException: 'Service request failed.
Status: 403 (Forbidden)

我已授予管理身份 Azure 应用程序配置数据权限

这是我应该使用的 clientId 吗?

更新:

我刚刚尝试使用我的活动目录(AAD --> 属性)的 ID,我得到一个

Azure.RequestFailedException: 'Service request failed.
Status: 403 (Forbidden)

这只能意味着我使用了错误的 ID,否则它应该像我看到的其他错误一样返回 400(错误请求)。

完整代码

private static async Task Main()
    {
        var builder = new ConfigurationBuilder();

        const string clientId = "e589d9f1-xxxx-xxxx-xxxx-6bc940d50ab7";

        builder.AddAzureAppConfiguration(options =>
        {
            options.Connect(new Uri("https://myconfigstore.azconfig.io"), new ManagedIdentityCredential(clientId));
        });

        _configuration = builder.Build();

        Console.WriteLine("Number of keys: " + _configuration.GetChildren().Count());

        Console.WriteLine("Demo: " + _configuration["Demo"]);
    }

本文档演示了如何使用托管标识从应用服务访问应用配置,但您可以将应用服务替换为支持托管标识的任何其他 Azure 服务。 https://docs.microsoft.com/en-us/azure/azure-app-configuration/howto-integrate-azure-managed-service-identity

以下是我想说的几件事

  • 确保在运行应用程序的 Azure 服务中启用托管标识。
  • 当您使用系统分配的托管身份时,您不需要提供客户端 ID。当您使用 用户分配 托管身份时,您只需提供客户端 ID。
  • 确保托管身份在访问权限中被授予 应用程序配置数据 Reader应用程序配置数据所有者 角色控制您的应用程序配置实例。
  • 角色分配后至少等待 15 分钟,以便传播权限。
  • 托管身份仅在您的代码在 Azure 服务中为 运行 时才有效。当 运行 在本地时它将不起作用。