azure - 部署时尝试运行计时器触发的功能时出现环境凭据身份验证错误

标签 azure azure-functions

我有一个计时器触发的函数,可以在本地运行 func,但是在发布时出现错误

[Error] Executed 'AppServiceSupervisorFunc' (Failed, Id=7xxxx-4f53-aee2-25241792cab8, Duration=6576ms)EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information

可能产生此错误的前几行代码是

    var monitoringClient = await GetMonitorClientAsync();
        ArmClient client = new ArmClient(new DefaultAzureCredential());
        string resourceGroupName = Environment.GetEnvironmentVariable("ResourceGroupName");

  private async Task<MonitorManagementClient> GetMonitorClientAsync()
    {
        var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync("xxxthcare.onmicrosoft.com", "8xxx-xxx-ad2b-8f608daf80db", "RPt8Q~JxxxUjbKxxx0TFaUH");
        var monitorClient = new MonitorManagementClient(serviceCreds);
        monitorClient.SubscriptionId = "xxx77-427b-8cdd-6fd9343040ab";
        return monitorClient;
    }

有什么建议吗?

更新 我启用了应用程序见解,看起来在 groupList.GetAsync 调用中生成了错误。

AppServiceSupervisorFunc DefaultAzureCredential failed to retrieve a token from the included credential

            var SubscriptionResourceId = new ResourceIdentifier(Environment.GetEnvironmentVariable("SubscriptionResourceId"));

        SubscriptionResource sub = client.GetSubscriptionResource(SubscriptionResourceId);
        var groupList = sub.GetResourceGroups();
        var group = await groupList.GetAsync(resourceGroupName);

enter image description here

更新2 当我使用时

  ArmClient client = new ArmClient(new AzureCliCredential());

我在 Azfuction 的控制台中收到 Azure CLI 未安装错误

最佳答案

  • 原因DefaultAzureCredential不起作用是因为 DefaultAzureCredential当您登录 Microsoft 帐户、azure cli 等时,使用存储在缓存中的凭据。但对于函数应用程序,缓存中没有凭据。

  • 因此,要使DefaultAzureCredential工作时,您必须在 azure AD 中注册一个应用程序,并从该应用程序中注册客户端 ID、租户 ID 和客户端凭据才能使用 DefaultAzureCredential .

  • 首先在 Azure 广告中注册您的应用程序,以便在门户中找到 azure ad并转到 App Registration 下然后点击New Registration .

enter image description here

  • 填写后续表格,然后点击“注册”。

enter image description here

  • 现在在列表中找到您最近注册的应用并点击它。

enter image description here

  • 然后复制 ClientidTenant id来自该应用程序。

enter image description here

  • 现在通过 Certificates & secret 生成客户端 key 选项卡并单击 New client secret

enter image description here

  • 填写后续表格并单击“添加”,然后您将在列表中看到新生成的客户端 key 。

enter image description here

  • 复制新生成的客户端 key

enter image description here

  • 现在您必须添加所有复制的 Clientid , tenantid , client secretConfiguration下的功能的应用程序设置中选项卡,只需点击新应用设置即可。

enter image description here

使用以下名称:

AZURE_CLIENT_ID

AZURE_CLIENT_SECRET

AZURE_TENANT_ID

现在在应用程序设置中添加之前复制的字符串(clientid、tenantid 等)。

enter image description here

  • 现在DefaultAzureCredential可以工作并提供身份验证 token 。'

  • 还有AzureCliCredential需要 azure cli,您必须先登录。这就是它给你错误的原因。

关于azure - 部署时尝试运行计时器触发的功能时出现环境凭据身份验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73981945/

相关文章:

c# - 在 Azure-Sql 数据库上运行到 "Master"的连接

azure - 单个 Azure 负载均衡器后面有多个 vmss

c# - 连接到 Azure 数据库的 Azure Function App C# HTTP 触发器

azure - 如何在Azure功能上运行ReactJS应用程序?

c# - 如何动态地将文件添加到存储在 Azure Blob 存储中的 zip 存档中?

azure - 如何防止逻辑应用(标准)中调用的工作流操作重试

azure - 预配和配置 - Azure 云

azure-functions - Azure Function Apps - 在管理门户中查看当前正在运行的实例数量

security - 保护 Azure Functions 和本地到 Azure Sql 的调用

Azure Function App 创建无法选择现有存储帐户