gpt4 book ai didi

c# - ASP.NET 核心 : IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:33 24 4
gpt4 key购买 nike

我正在将我的 ASP.NET Core 网络应用程序升级到 .NET Core 2.1,但出现以下错误:

IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault

此代码适用于 .NET Core 2.0。 .NET Core 2.1 中似乎缺少一个库。

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;

namespace MyNamesace
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, builder) =>
{
var keyVaultEndpoint = GetKeyVaultEndpoint();
if (!string.IsNullOrEmpty(keyVaultEndpoint))
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

builder.AddAzureKeyVault(keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
}
})
.UseStartup<Startup>();
}

static string GetKeyVaultEndpoint() => Environment.GetEnvironmentVariable("KeyVaultEndpoint");
}
}

最佳答案

回答

安装 Microsoft.Extensions.Configuration.AzureKeyVault NuGet 包。

无需更改代码。

更新:Microsoft.Extensions.Configuration.AzureKeyVault NuGet 包已弃用。新的 NuGet 包是 Azure.Extensions.AspNetCore.Configuration.Secrets .

说明

ASP.NET Core 2.0 模板使用了 Microsoft.AspNetCore.All NuGet 包,其中包括 IConfiguration 的 Azure Key Vault 扩展,但 ASP.NET Core 2.1 模板使用 Microsoft.AspNetCore.App其中不包括扩展库。

代码

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureKeyVault;

namespace MyNamespace
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((ctx, builder) =>
{
var keyVaultEndpoint = GetKeyVaultEndpoint();
if (!string.IsNullOrEmpty(keyVaultEndpoint))
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));

builder.AddAzureKeyVault(keyVaultEndpoint, keyVaultClient, new DefaultKeyVaultSecretManager());
}
})
.UseStartup<Startup>();
}

static string GetKeyVaultEndpoint() => Environment.GetEnvironmentVariable("KeyVaultEndpoint");
}
}

关于c# - ASP.NET 核心 : IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52579867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com