Azure AppConfiguration(在本地使用 DefaultAzureCredential)不返回任何密钥

时间:2021-03-18 06:59:26

标签: azure azure-function-app azure-app-configuration

当我在函数中注入 IConfiguration 时,它找不到任何仅存在于我的“Azure 应用程序配置”中的键。

我有一个使用 DefaultAzureCredential 访问应用程序配置的 functionApp (V3)。我在调试中在本地运行它,因此需要默认凭据。我也有多个租户,所以我必须在 DefaultAzureCredentialOptions 上设置 VisualStudioTenantId 和 SharedTokenCacheTenantId。我的 Visual Studio 用户还获得了“应用配置数据读取器”角色,以便能够进行调试。

连接到应用程序配置时,我没有收到任何错误。 编辑添加:我已设置 AppConfiguration 以使用 AzureAD 进行身份验证。

见下面的代码:

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>");

    /*Works but where are my keys when I try to access them?*/
    builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
    {
        options.Connect(new Uri(appConfigURI), cred);
        
    }).Build(); //Should I be building this??

}

在我的函数中

public FunctName(IConfiguration configuration)
{
    _configuration = configuration;
}

当我访问该属性时

var prop = _configuration["PropertyName"];

2 个答案:

答案 0 :(得分:0)

此处 https://github.com/Azure/AppConfiguration/blob/main/examples/DotNetCore/AzureFunction/FunctionApp/Startup.cs 有一个使用 IFunctionsConfigurationBuilder 的示例函数应用。我建议您看一下,看看是否有任何缺失的部分。

标题提到“在本地使用 DefaultAzureCredential”。这是否意味着如果您使用连接字符串,这会按预期工作吗?

答案 1 :(得分:0)

注意 async 无效的 ConfigureAppConfiguration。这导致我的 ConfigureAppConfiguration 无法同步执行,导致 configure 在填充之前添加​​我的应用配置。