我有一個函式應用程式,需要授予它寫入 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 的回答,我似乎不需要它們
我發現一件非常令人困惑的事情(因為我在任何地方都找不到好的檔案)是Azure要求我包含三個變數
- AZURE_CLIENT_ID
- AZURE_CLIENT_SECRET
- 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 設定為應用注冊的機密,將 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
老實說,我現在很困惑,有人可以幫助我了解如何成功設定系統分配的托管身份嗎?
UPDATE WITH ANSWER 得到了 Peter 的建議。所以,我不是 C# 開發人員,但有編程背景并且正在為這個專案做 devops,另一組正在撰寫應用程式。我沒有意識到他們專門使用了 new EnvironmentCredential(); 在他們的代碼中,因為他們強調始終需要使用 DefaultAzureCredential 但 EnvironmentCredential 強制設定 AZURE_CLIENT_ID。這也解釋了我們使用 DefaultAzureCredential 的問題,因為它像您在答案中鏈接的那樣遍歷串列,因此它看到 AZURE_CLIENT_ID 已設定,并且即使 MI 具有正確的權限也不會打擾 MI。
uj5u.com熱心網友回復:
使用托管標識時,您不應提供這些環境變數。它還會破壞不必存盤秘密的目的。在這種情況下,它們不是必需的。這只是DefaultAzureCredential嘗試獲取有效憑據的眾多方法之一。請參閱本節。如果您不提供它們,它將回退嘗試使用托管標識。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520710.html
標籤:C#天蓝色验证天蓝色函数azure-managed-identity
上一篇:是否可以使用通過API密鑰進行身份驗證的TwilioSMSAPI?
下一篇:next-authGET/api/auth/session400CLIENT_FETCH_ERROR意外令牌“E”(Netlify部署)
