首页 > 解决方案 > 授予 Azure Function 对 ARM REST API 的访问权限

问题描述

今年早些时候,我创建了一个 Azure 函数,它使用 Azure REST API 以编程方式复制数据库。这有效,但现在由于某种原因停止工作。

要进行身份验证,Azure 函数中的代码会获取一个身份验证令牌...

var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com/");
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

我们在 Azure 门户中进行了其他操作,以授予 Azure Function 执行此操作的权限。我记得从下拉列表中选择了 Azure 函数本身(我认为是在其中一个Access Control (IAM)面板中)。不幸的是,我不记得我做了什么。

这突然停止工作,Azure Function 无法说......

Parameters: Connectionstring: [No connection string specified], Resource: https://management.azure.com/, Authority: . Exception Message: Tried the following 4 methods to get an access token, but none of them worked.
Parameters: Connectionstring: [No connection string specified], Resource: https://management.azure.com/, Authority: . Exception Message: Tried to get token using Managed Service Identity. Unable to connect to the Managed Service Identity (MSI) endpoint. Please check that you are running on an Azure resource that has MSI setup.
Parameters: Connectionstring: [No connection string specified], Resource: https://management.azure.com/, Authority: . Exception Message: Tried to get token using Visual Studio. Access token could not be acquired. Visual Studio Token provider file not found at "D:\local\LocalAppData\.IdentityService\AzureServiceAuth\tokenprovider.json"
Parameters: Connectionstring: [No connection string specified], Resource: https://management.azure.com/, Authority: . Exception Message: Tried to get token using Azure CLI. Access token could not be acquired. 'az' is not recognized as an internal or external command,
operable program or batch file.

Parameters: Connectionstring: [No connection string specified], Resource: https://management.azure.com/, Authority: https://login.microsoftonline.com/common. Exception Message: Tried to get token using Active Directory Integrated Authentication. Access token could not be acquired. get_user_name_failed: Failed to get user name
Inner Exception : The format of the specified domain name is invalid

调用堆栈显示它在AzureServiceTokenProvider.GetAccessTokenAsync()我上面的代码片段中出错。

我没有更改代码中的任何内容,但团队中的其他人可能在 Azure 中重新创建了一些东西 - 导致我们在门户中所做的任何事情(如上所述)都需要重做。

我也找不到这样做时我最初会关注的文章。无论我去哪个 IAM 面板(无论是订阅本身,还是 sql server 实例),我都可以选择Assign assess to Function App,但它总是显示No Function App in this resource group can be assigned access..

在此处输入图像描述

有什么我想念的想法吗?如何授予 Function App 向数据库管理发出资源管理器 REST 请求的权限?

标签: azureazure-functionsazure-resource-managerazure-sql-server

解决方案


根据您的描述,Azure 功能 MSI 似乎已关闭。您可以尝试使用 Azure 功能平台功能打开 MSI 功能。

在此处输入图像描述

如果我们要操作 Azure 数据库,正如您提到的,我们需要为 Azure 数据库服务器设置 IAM 以允许 azure 函数操作数据库资源。

如果 MSI 在 Azure 功能上,我们可以在下拉列表中找到 Azure 功能。

在此处输入图像描述

我也为它做了一个演示。我通过服务器 API测试列表数据库。它在我这边正常工作。

在此处输入图像描述


推荐阅读