簡體   English   中英

具有用戶分配的托管標識的 Azure 應用服務使應用程序崩潰

[英]Azure App Service with User-Assigned Managed Identity crashes application

我有一個 VMSS 和多個 AppService,我想使用相同的用戶分配的托管服務標識。 對於 VMSS,我可以分配標識並使用它從 Azure Key Vault 中檢索機密,代碼如下:

var client = new SecretClient(new Uri(KeyVault), new DefaultAzureCredential());
var secret = client.GetSecret("secret-name");

AppServices 使用 ASP.NET Core 3.1,因此推薦的訪問 Key Vault 機密的方法是:

var azureServiceTokenProvider = new AzureServiceTokenProvider("RunAs=App;AppId={client id for the user-assigned managed identity elided}");
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
config.AddAzureKeyVault("https://{my vault name}.vault.azure.net/", keyVaultClient, new DefaultKeyVaultSecretManager());

請注意,連接字符串派生自本文檔中的“用戶分配的 Azure 資源標識”方案。

上面的代碼片段拋出以下異常:

2020-08-27T02:06:18.409648197Z 未處理的異常。 System.ArgumentException:連接字符串 RunAs=App;AppId={client id ellided} 無效。 必須包含“TenantId”屬性並且不能為空。 2020-08-27T02:06:18.409681697Z at Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderFactory.ValidateAttribute(Dictionary`2 connectionSettings, String attribute, String connectionString) at Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderFactory.ValidateAttribute(Dictionary`2 connectionSettings, String attribute, String connectionString) 2020-08-27T02:06:18s.407Z.968 .AppAuthentication.AzureServiceTokenProviderFactory.Create(String connectionString, String azureAdInstance) 2020-08-27T02:06:18.409693297Z 在 Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider..ctor(String connectionString, String azureAdInstance) 20207002:82070 :18.409697797Z at API.Program.<>c.b__1_0(HostBuilderContext context, IConfigurationBuilder config) in /tmp/8d84a2d16145d21/API/Program.cs:line 25 2020-08-27T02:06:103.49797 .HostBuilder.BuildAppConfiguration() 2020-08-27T02:06:18.409707797Z 在 Microsoft.Extensions.Hosting.HostBuilder.Build()

當我按要求添加“TenantId”時,消息更改為:

未處理的異常。 System.ArgumentException: 連接字符串 RunAs=App;AppId={client id elided};TenantId={tenant id elided} 無效。 必須至少包含 CertificateStoreLocation 或 AppKey 屬性之一。

托管身份沒有證書,我正在嘗試使用 MSI 來避免向代碼或應用程序設置添加機密。

我已嘗試根據“Azure 資源的托管身份”方案刪除連接字符串的“AppId”和“TenantId”部分,這導致以下異常:

未處理的異常。 Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException:參數:連接字符串:RunAs=App,資源: https ://vault.azure.net,權限: https : //login.windows.net/b905ac32-5779-4bab-ac34 -a8445e89f9e4 異常消息:嘗試使用托管服務標識獲取令牌。 無法獲取訪問令牌。 MSI ResponseCode: BadRequest, Response: {"statusCode":400,"message":"無法加載請求的托管標識。","correlationId":"c8409322-357a-49d0-9686-453fb37cc4b4"}

我假設它正在嘗試加載不存在的系統分配的身份。 我已經確認通過 Kudu 控制台為 (Linux) WebApp 實例配置了托管標識:

  Kudu Remote Execution Console Type 'exit' to reset this console.
  /home>env
  MSI_ENDPOINT=[Managed identity has been configured. This value is not viewable in Kudu but is exposed to the app.]
  IDENTITY_ENDPOINT=[Managed identity has been configured. This value is not viewable in Kudu but is exposed to the app.]
  IDENTITY_HEADER=[Managed identity has been configured. This value is not viewable in Kudu but is exposed to the app.]
  MSI_SECRET=[Managed identity has been configured. This value is not viewable in Kudu but is exposed to the app.]

MSI+AppService+Linux、文檔、或兩者、代碼示例、配置或我的代碼是否有問題?

注意:不再建議將 Microsoft.Azure.Services.AppAuthentication 與新的 Key Vault SDK 一起使用。 它被可用於 .NET、Java、TypeScript 和 Python 的新 Azure 標識庫 DefaultAzureCredentials 取代,並且應用於所有新開發。 可以在此處找到更多信息: 身份驗證和 Azure SDK

您發布的 VMSS 代碼使用的是新的 KeyVault SDK,這很好。

但是對於應用服務,由於您仍然使用舊版 SDK(假設通過查看您的代碼),請檢查您是否已將Microsoft.Azure.Services.AppAuthentication的最新穩定 nuget 明確添加到您的項目中(假設您繼續使用現在是舊版 SDK)。

另外,我希望您已經在應用服務刀片中添加了用戶分配的身份。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM