使用 Azure KeyVault 和 Visual Studio 管理用户机密不适用于 ASP .Net Core

Managing User Secrets with Azure KeyVault and Visual Studio not working for ASP .Net Core

基于 here and here

中的步骤

在连接服务中添加新的 KeyVault 时出现以下错误。我正在使用 VS 15.8.1 社区版。 asp.net核心2.1

"Could not retrive Key Vault information. The hyperlink on this page might not work as expected and UI might have stale or no information. Please check the activity log for more information. C:\Users\kumar\AppData\Roaming\Microsoft\VisualStudio.0_536ef4bf\ActivityLog.xml"

我不会在 appsetting.json 中因为将其设置为 Azure Key Vault 而变得波浪形。

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "dummyaapinsightkey-cxds3-4001-953f-sf323sdfw32d"
  }
}

我不确定这个错误。看起来有些连接问题。

但不要使用 KeyVault 作为 连接服务 我建议您可以使用 KeyVault 的可配置模型(没有连接服务)来检索秘密

通过使用连接服务,很难在部署到生产环境时更改 KeyVault 的配置

written an article 您可以在其中使用 AzureKeyVault 检索您的机密,而无需使用连接服务

安装以下 NuGet 包

Install-Package Microsoft.Extensions.Configuration.AzureKeyVault -Version 2.1.1

您只需将 Program.cs 更改为如下所示

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
             WebHost.CreateDefaultBuilder(args)
                 .ConfigureAppConfiguration((context, config) =>
                 {
                     var builtConfig = config.Build();

                     config.AddAzureKeyVault(
                         $"https://{builtConfig["Vault"]}.vault.azure.net/",
                         builtConfig["ClientId"],
                         builtConfig["ClientSecret"]);
                 })
                 .UseStartup<Startup>();
    }

appsetting.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "Vault": "myKeyVault",
  "ClientId": "xxxxx-xxx-xxx-xxx-xxxx",
  "ClientSecret": "xxxxxxx="
}

更新:

现在使用 Managed Identity,您可以轻松集成 Keyvault,甚至无需使用 ClientId 和 Client Secrets

阅读更多关于:https://docs.microsoft.com/en-us/samples/azure-samples/app-service-msi-keyvault-dotnet/keyvault-msi-appservice-sample/