Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core: IConfigurationBuilder Does Not Contain Definition For AddAzureKeyVault

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");
    }
}
like image 243
Brandon Minnick Avatar asked Dec 07 '22 13:12

Brandon Minnick


1 Answers

Answer

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.

Explanation

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.

Code

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");
    }
}
like image 191
Brandon Minnick Avatar answered Jan 07 '23 10:01

Brandon Minnick