c# - Azure - 系统为 Function App 分配托管标识

标签 c# azure authentication azure-functions azure-managed-identity

我有一个函数应用程序,需要授予它写入 blob/表存储的权限。我打开了“系统分配的托管身份”,并为其设置了以下权限,范围是我需要访问的存储帐户。

Storage Account Contributor
Storage Blob Data Owner
Storage Table Data Contributor
Storage Queue Data Contributor

更新 我删除 AZURE_CLIENT_ID、AZURE_CLIENT_SECRET 和 AZURE_TENANT_ID,然后收到环境配置错误。我没有在本地运行或调试,而是通过 API 管理触发它。

Executed 'Create' (Failed, Duration=1406ms)EnvironmentCredential authentication unavailable. Environment variables are not fully configured. See the troubleshooting guide for more information. https://aka.ms/azsdk/net/identity/environmentcredential/troubleshoot

^ 上面的链接告诉我应该将这些环境变量添加回来,但根据下面 Peter Bon 的回答,我似乎不需要它们

我发现非常令人困惑的一点(因为我在任何地方都找不到关于这方面的好的文档)是我 required通过 Azure 包含三个变量

  1. AZURE_CLIENT_ID
  2. AZURE_CLIENT_SECRET
  3. AZURE_TENANT_ID

我推断它们实际上需要允许我的函数应用程序访问存储帐户,这一开始让我感到困惑,因为如果我只是使用分配的角色明确授予它权限,为什么我还需要创建完全在外部的东西我的功能应用程序是否允许其执行我已授予其执行权限的操作?

我对此的问题是“我该如何做这个?”如果我需要进行应用程序注册,回调 URL 应该是什么? API 权限应该是什么?我尽了最大的猜测,但没有得到满意的结果。

App Registration:
API permissions -> Microsoft.Graph User.Read
Authentication -> https://<mydomain>.onmicrosoft.com/.auth/login/aad/callback
               -> ID Tokens
               -> Accounts in this organization
Secrets        -> Generated new secret to use for AZURE_CLIENT_SECRET
Roles & Admissions -> Cloud application administrator

然后,我将 AZURE_CLIENT_ID 设置为此应用注册的应用 ID,将 AZURE_CLIENT_SECRET 设置为应用注册的 key ,将 AZURE_TENANT_ID 设置为我的租户 ID。

然后在我的代码中,我尝试执行以下操作

var tableUri = new Uri(string.Format("https://{0}.table.core.windows.net/", storageAccountName));
var credential = new DefaultAzureCredential(options);
services.AddScoped(x => new TableServiceClient(tableUri, credential));

访问我的表存储时失败并出现以下错误:

Executed 'Create' (Failed, Id=<id>, Duration=2108ms)Server failed to authenticate the request. Please refer to the information in the www-authenticate header.RequestId:<id>Time:2022-10-21T12:15:21.6998519ZStatus: 401 (Server failed to authenticate the request. Please refer to the information in the www-authenticate header.)ErrorCode: InvalidAuthenticationInfoContent:{"odata.error":{"code":"InvalidAuthenticationInfo","message":{"lang":"en-US","value":"Server failed to authenticate the request. Please refer to the information in the www-authenticate header.\nRequestId:<id>\nTime:2022-10-21T12:15:21.6998519Z"}}}Headers:Server: Microsoft-HTTPAPI/2.0x-ms-request-id: <id>x-ms-error-code: REDACTEDWWW-Authenticate: Bearer authorization_uri=https://login.microsoftonline.com/<tenant_id>/oauth2/authorize resource_id=https://storage.azure.comDate: Fri, 21 Oct 2022 12:15:21 GMTContent-Length: 279Content-Type: application/json

如果我将身份验证重定向更新为

https://storage.azure.com

然后我收到以下错误: Executed 'Create' (Failed, Id=<id>, Duration=2349ms)This request is not authorized to perform this operation using this permission.RequestId:<request>Time:2022-10-21T13:14:29.0955823ZStatus: 403 (Forbidden)ErrorCode: AuthorizationPermissionMismatchContent:{"odata.error":{"code":"AuthorizationPermissionMismatch","message":{"lang":"en-US","value":"This request is not authorized to perform this operation using this permission.\nRequestId:<id>\nTime:2022-10-21T13:14:29.0955823Z"}}}Headers:Cache-Control: no-cacheTransfer-Encoding: chunkedServer: Windows-Azure-Table/1.0,Microsoft-HTTPAPI/2.0x-ms-request-id: <id>x-ms-client-request-id: <id>x-ms-version: REDACTEDX-Content-Type-Options: REDACTEDDate: Fri, 21 Oct 2022 13:14:28 GMTContent-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8

老实说,我现在很困惑,有人可以帮助我了解如何成功设置系统分配的托管身份吗?

更新答案 根据 Peter 的建议解决了这个问题。所以,我不是 C# 开发人员,但有编程背景并且正在为这个项目进行 DevOps,另一个小组正在编写应用程序。我没有意识到他们专门使用了 new EnvironmentCredential();在他们的代码中,因为他们强调始终需要使用 DefaultAzureCredential,但 EnvironmentCredential 强制设置 AZURE_CLIENT_ID。这也解释了我们使用 DefaultAzureCredential 的问题,因为它会像您在答案中链接的那样遍历列表,因此它会看到 AZURE_CLIENT_ID 已设置,并且即使 MI 具有正确的权限,也不会费心去寻找 MI。

最佳答案

使用托管身份时,不应提供这些环境变量。这也会违背不必存储 secret 的目的。在这种情况下不需要它们。这只是 DefaultAzureCredential 尝试获取有效凭据的多种方式之一。请参阅this section 。如果您不提供它们,它将回退尝试使用托管身份。

关于c# - Azure - 系统为 Function App 分配托管标识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74154342/

相关文章:

azure - 截至 2020 年,对通过 Azure Web App 上传到 Azure block 存储的文件进行病毒扫描

authentication - Auth0 的优点

sql-server - sequelize azure sql 数据库回滚

ios - 需要帮助了解应用程序的 ios http 身份验证

angularjs - RESTful token 身份验证说明

c# - 在面板中添加新标签时出现 NullReferenceException

c# - 为什么实例化一个对象不自动实例化它的属性?

c# - 如何在接口(interface)中将方法设为私有(private)?

c# - 存储过程问题

azure - ADF 链接服务连接字符串更新