0

我有一个 functionApp (V3) 正在尝试使用 DefaultAzureCredential 访问应用程序配置。我的系统管理用户已打开并具有“应用程序配置数据阅读器”角色。我在调试中本地运行它,因此需要默认凭据。我也有多个租户,所以我必须在 DefaultAzureCredentialOptions 上设置 VisualStudioTenantId 和 SharedTokenCacheTenantId。

该凭证在访问 Key vault 以获取机密时有效,但需要设置 SharedTokenCacheTenantId。

连接到应用程序配置时,我收到“服务请求失败。状态:403(禁止)。

请参见下面的代码:

public override async void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    var credOptions = new DefaultAzureCredentialOptions();

    var tenantId = Environment.GetEnvironmentVariable("Tenant_Id");

    credOptions.VisualStudioTenantId = tenantId;
    
    credOptions.SharedTokenCacheTenantId = tenantId;

    var cred = new DefaultAzureCredential(credOptions);

    /*Works but requires SharedTokenCacheTenantId*/
    var secretClient = new SecretClient(new Uri(vaultURI), cred);
    var secret = await secretClient.GetSecretAsync("<secret name>");

    /*Does not work - forbidden*/
    builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
    {
        options.Connect(new Uri(appConfigURI), cred);
        
    }).Build();

}

谢谢!

4

1 回答 1

2

感谢zhenlan在这里回答我的问题。我的本地 Visual Studio 用户也需要被授予“应用程序配置数据阅读器”,才能在本地调试我的代码。

于 2021-03-17T19:47:43.937 回答