azure - 授予 Azure Function 对 ARM REST API 的访问权限

标签 azure azure-functions azure-resource-manager azure-sql-server

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

为了进行身份验证,Azure Function 中的代码会获取一个身份验证 token ...

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

我们在 Azure 门户中做了其他一些事情来向 Azure Functions 授予执行此操作的权限。我记得从下拉列表中选择了 Azure Function 本身(我认为是在访问控制 (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 实例),我都可以选择将评估分配给 Function App,但它总是显示此资源中没有 Function App可以为组分配访问权限。.

enter image description here

我缺少什么想法吗?如何授予函数应用向数据库管理发出资源管理器 REST 请求的权限?

最佳答案

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

enter image description here

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

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

enter image description here

我还为它做了一个演示。我测试list databases by server API 。它在我这边工作正常。

enter image description here

关于azure - 授予 Azure Function 对 ARM REST API 的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51037330/

相关文章:

Azure 数据工厂 - 分页

c# - Azure Service Fabric Actor 初始化

azure - 属性值不适当 "network_interface_id": string required

azure-devops - 如何创建利用 Key Vault 的幂等、可重新部署的 ARM 模板?循环依赖存在问题

api - 如何使用 Azure 资源管理器在 Azure API 管理中对 API 进行版本控制

azure - ARM 模板 - 不允许主模板中存在多个链接模板

encryption - 保护 Windows Azure 中的连接字符串错误 : Failed to Decrypt

azure - 查看实时指标时如何防止水平自动滚动?

c# - 调试测试时从 local.setting.json 读取值

azure - Azure Function 中没有从表到 ITableEntity 的隐式引用转换