具有 App Config 和托管身份的 Azure Functions - 如何在本地调试

Azure Function with App Config and managed identity - how to debug locally

我到处寻找标题中问题的答案,但我找到了 0 个关于我的问题的相关信息,是的,有其他服务的例子,但没有我的例子。

我有一个 Azure 应用程序功能。
我启用了它的托管身份。
我在应用程序配置访问管理 (IAM) 中为该功能添加了一个 data-reader 角色。
在我的代码中添加了

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var appConfigEndpoint = Environment.GetEnvironmentVariable("AppConfigEndpoint");
        builder.ConfigurationBuilder.AddAzureAppConfiguration(options => options.Connect(new Uri(appConfigEndpoint), new ManagedIdentityCredential()));
    }

在我的 Startup.cs

当我在本地 DEBUG 中 运行 它时,我得到这个错误:

A host error has occurred during startup operation '<some GUID>'.
Azure.Identity: ManagedIdentityCredential authentication unavailable. No Managed Identity endpoint found.
Value cannot be null. (Parameter 'provider')

也尝试将 new ManagedIdentityCredential() 更改为 new DefaultAzureCredential() 但后来我得到:

A host error has occurred during startup operation '732e2be5-0729-4279-b15a-833c8313b39a'.
Azure.Data.AppConfiguration: Service request failed.
Status: 403 (Forbidden)

Headers:
Server: openresty/1.17.8.2
Date: Wed, 30 Jun 2021 14:15:22 GMT
Connection: keep-alive
x-ms-request-id: 2f2066ef-9222-42d5-b96b-85a9b338127e
x-ms-client-request-id: 68cd19e1-0166-4567-a3f9-10f9a1cb6f3c
x-ms-correlation-request-id: 2f2066ef-9222-42d5-b96b-85a9b338127e
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate
Strict-Transport-Security: max-age=15724800; includeSubDomains
Content-Length: 0
.
Value cannot be null. (Parameter 'provider')

我的 Azure 帐户已添加到我的 Visual Studio 并在“工具”>“选项”>“Azure 服务身份验证”中选中它。

我快疯了 - 我必须做什么才能在本地进行调试?

ManagedIdentityCredential 仅适用于 Azure。 您可以将其改为使用 DefaultAzureCredential。 它尝试多种方法,包括托管身份和 Visual Studio 身份验证。 所以它也会在本地工作。 当然,您的用户帐户也需要具有所需的 RBAC 角色。