簡體   English   中英

.NET 核心應用在生產中找不到上傳的證書 [Azure Key Vault]

[英].NET Core app can't find uploaded certificate in production [Azure Key Vault]

我最近閱讀了很多關於使用 Azure Key Vault 管理機密的內容。 我設法使用 Ubuntu 20.04 在服務器中創建和安裝 a.pfx 證書,並按照這些步驟將證書上傳到我的 Azure AD。

在連接到我的 Key Vault 之前正確找到了證書,並且在我從 Windows 和 Linux (WSL) 開發時檢索了機密。 但是,當我將應用程序部署到我的生產服務器時,我創建的用於管理 kestrel 的服務會引發“core-dump”錯誤,類似於這個問題

但就我而言,當我查看日記時,我發現以下內容:

Unhandled exception. System.InvalidOperationException: Sequence contains no elements

令人驚訝的是,如果我只是使用“dotnet app.dll”手動運行應用程序,這不會發生。

這怎么可能? 如果我手動運行它,它會打開商店,找到證書並訪問機密,但在服務運行時找不到任何東西。

這是我用來在我的 Program.cs 中配置對 Key Vault 的訪問的相關代碼:

// Azure Key Vault configuration.
using var store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certs = store.Certificates.Find(X509FindType.FindByThumbprint, configuration["KeyVault:AzureADCertThumbprint"], false);

configuration.AddAzureKeyVault(
    new Uri($"https://{configuration["KeyVault:KeyVaultName"]}.vault.azure.net/"),
    new ClientCertificateCredential(configuration["KeyVault:AzureADDirectoryId"], configuration["KeyVault:AzureADApplicationId"], certs.OfType<X509Certificate2>().Single()),
    new KeyVaultSecretManager());

store.Close();

誰能幫我找到問題? 提前致謝。

  • 我檢查了一些 Microsoft 文檔,但沒有發現訪問密鑰庫的代碼有任何問題。 我猜您可能會忘記門戶上的application setting中的某些內容,因為您的代碼沒有問題。

  • 創建證書后,配置 Azure AD 並關聯證書,之后我們可以使用 X509 證書從 .NET 客戶端訪問 Azure Key Vault

  • 導入證書並將證書數據投射到 Base64 后,我們應該創建 Azure 資源管理器 AD 應用程序和服務主體。 為 Azure Key Vault 成功配置 Azure 資源管理器應用程序和服務主體可以解決您面臨的問題。

在此處輸入圖像描述

檢查此Accessing Azure Key Vaults Using Certification and Setup Key Vault using Azure AD Application and Certificates了解更多信息。

暫無
暫無

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

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