public static IConfigurationBuilder AddAMyAzureKeyVault(this IConfigurationBuilder configurationBuilder)
        {
            var vaultEndpoint                = "https://benjamintestvault.vault.azure.net/";
            var azureServiceTokenProvider    = new AzureServiceTokenProvider();
            var defaultKeyVaultSecretManager = new DefaultKeyVaultSecretManager();
            var authentificationCallback     = new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback);
            var keyVaultClient               = new KeyVaultClient(authentificationCallback);

            return(configurationBuilder.AddAzureKeyVault(vaultEndpoint, keyVaultClient, defaultKeyVaultSecretManager));
        }
Exemplo n.º 2
0
        public static IConfigurationRoot AddConfiguration(this IServiceCollection services, Assembly assembly)
        {
            var providers = new List <IConfigurationProvider>();

            var configServiceDescriptors =
                services.Where(descriptor => descriptor.ServiceType == typeof(IConfiguration))
                .ToList();

            foreach (var descriptor in configServiceDescriptors)
            {
                if (!(descriptor.ImplementationInstance is IConfigurationRoot existingConfiguration))
                {
                    continue;
                }

                providers.AddRange(existingConfiguration.Providers);
                services.Remove(descriptor);
            }

            var serviceProvider  = services.BuildServiceProvider();
            var executionContext = serviceProvider.GetService <IOptions <ExecutionContextOptions> >().Value;

            var builder = new ConfigurationBuilder()
                          .SetBasePath(executionContext.AppDirectory)
                          .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                          .AddEnvironmentVariables();

            var aspCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            if (string.Equals(aspCoreEnvironment, "Development",
                              StringComparison.InvariantCultureIgnoreCase))
            {
                builder.AddUserSecrets(assembly, optional: true);
            }
            else
            {
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var authenticationCallback    = new KeyVaultClient.AuthenticationCallback(
                    azureServiceTokenProvider.KeyVaultTokenCallback);
                var keyVaultClient = new KeyVaultClient(authenticationCallback);
                var defaultKeyVaultSecretManager = new DefaultKeyVaultSecretManager();
                builder.AddAzureKeyVault("https://socialnetworkapp-0-kv.vault.azure.net/",
                                         keyVaultClient, defaultKeyVaultSecretManager);
            }

            var config = builder.Build();

            providers.AddRange(config.Providers);

            var configurationRoot = new ConfigurationRoot(providers);

            services.AddSingleton <IConfiguration>(configurationRoot);
            return(configurationRoot);
        }
Exemplo n.º 3
0
        private static IConfiguration AddAzureKeyVaultToConfiguration(IConfiguration configuration)
        {
            var configBuild = new ConfigurationBuilder().AddConfiguration(configuration);

            var azureServiceTokenProvider    = new AzureServiceTokenProvider();
            var keyVaultClient               = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
            var defaultKeyVaultSecretManager = new DefaultKeyVaultSecretManager();

            configBuild.AddAzureKeyVault(configuration[Constant.KeyVaultUri], keyVaultClient, defaultKeyVaultSecretManager);

            return(configBuild.Build());
        }
Exemplo n.º 4
0
        public static IHostBuilder CreateHostBuilder(string[] args) => Host
        .CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((webHostBuilderContext, configurationBuilder) =>
        {
            var vaultEndpoint                = "https://benjamintestvault.vault.azure.net/";
            var azureServiceTokenProvider    = new AzureServiceTokenProvider();
            var defaultKeyVaultSecretManager = new DefaultKeyVaultSecretManager();
            var authentificationCallback     = new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback);
            var keyVaultClient               = new KeyVaultClient(authentificationCallback);

            configurationBuilder.AddAzureKeyVault(vaultEndpoint, keyVaultClient, defaultKeyVaultSecretManager);
        })
        .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup <Startup>());
        private static IConfiguration BuildConfiguration(
            IServiceProvider serviceProvider)
        {
            IConfiguration toReturn = null;

            ConfigurationBuilder configurationBuilder =
                new ConfigurationBuilder();

            // Go to environment variables first...
            configurationBuilder.AddEnvironmentVariables();

            IStartupSettingsProvider startupSettingsProvider =
                serviceProvider.GetService <IStartupSettingsProvider>();

            string keyVaultInstanceName =
                startupSettingsProvider.KeyVaultInstanceName;

            if (!string.IsNullOrEmpty(keyVaultInstanceName))
            {
                string vault =
                    $"https://{keyVaultInstanceName}.vault.azure.net/";

                AzureServiceTokenProvider azureServiceTokenProvider =
                    new AzureServiceTokenProvider();

                TokenCallback keyVaultTokenCallback =
                    azureServiceTokenProvider.KeyVaultTokenCallback;

                KeyVaultClient.AuthenticationCallback authenticationCallback =
                    new KeyVaultClient.AuthenticationCallback(
                        keyVaultTokenCallback);

                KeyVaultClient keyVaultClient = new KeyVaultClient(
                    authenticationCallback);

                DefaultKeyVaultSecretManager defaultKeyVaultSecretManager =
                    new DefaultKeyVaultSecretManager();

                // Otherwise, KeyVault.
                configurationBuilder.AddAzureKeyVault(
                    vault,
                    keyVaultClient,
                    defaultKeyVaultSecretManager);
            }

            try
            {
                toReturn = configurationBuilder.Build();
            }
            catch (AzureServiceTokenProviderException azureServiceTokenProviderException)
            {
                throw new Exception(
                          $"This is likely happening because you're debugging, " +
                          $"and you haven't used the Azure CLI 2.0 tools to 'az " +
                          $"login'. Because KeyVault uses Managed Service " +
                          $"Identities, you need to do this first. If you'd " +
                          $"rather fall back to environment variables only, make " +
                          $"the setting value for " +
                          $"{nameof(IStartupSettingsProvider.KeyVaultInstanceName)} " +
                          $"null (or just omit it completely).",
                          azureServiceTokenProviderException);
            }

            return(toReturn);
        }