How to read secrets from Azure KeyVault and then update the configuration object in azure function app

195 views Asked by At

I have created function app with .net core 2.1 by using VS2017. I have followed this documentation to read settings from appsettings.json.

But I want to integrate Azure KeyVault with managed identity option, to read secrets from it and then update the configuration object.

So, can anyone suggest me on this issue?

1

There are 1 answers

0
Gaushick On

Add the below method in startup.cs file. Here KeyVaultName needs to be added in the Azure portal Configuration or local appsettings file.

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
    base.ConfigureAppConfiguration(builder);
    var builtConfig = builder.ConfigurationBuilder.Build();
    var secretClient = new SecretClient(
                new Uri($"https://{builtConfig["KeyVaultName"]}.vault.azure.net"),
                new DefaultAzureCredential(),
                new SecretClientOptions()
                {
                    Retry =
                        {
                                    Delay= TimeSpan.FromSeconds(2),
                                    MaxDelay = TimeSpan.FromSeconds(16),
                                    MaxRetries = 5,
                                    Mode = RetryMode.Exponential
                        }
                }
            );
            builder.ConfigurationBuilder.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
    }