I am upgrading my ASP.NET Core web app to .NET Core 2.1, but am getting the following error:
IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault
This code works in .NET Core 2.0. It looks like a library is missing in .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");
}
}
Install the Microsoft.Extensions.Configuration.AzureKeyVault NuGet Package.
No changes to the code are necessary.
UPDATE: The Microsoft.Extensions.Configuration.AzureKeyVault
NuGet Package has been deprecated. The new NuGet Packages is Azure.Extensions.AspNetCore.Configuration.Secrets.
ASP.NET Core 2.0 template used the Microsoft.AspNetCore.All NuGet package which includes the Azure Key Vault extensions for IConfiguration, but ASP.NET Core 2.1 template uses Microsoft.AspNetCore.App which does not include the extension library.
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With