Aller au contenu principal

Overriding Key Vault Settings for Local development

In some cases, you might want to develop using different settings than the ones provided in the Key Vault. In order to do so, you may add a file named "appsettings.local.json" and modify your startup program file to add the reference to the JSON file. Make sure to register the appsettings.local.json file after the key vault has been registered. Important: Add the appsettings.local.json to your gitignore file, we don't want to spread those settings to other developers.

// public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.AddAppSettingsSecretsJson()
.ConfigureAppConfiguration((_, builder) =>
{

var config = builder.Build();
var keyVaultUri = config["Settings:KeyVaultUri"];
builder.AddAzureKeyVault(new Uri(keyVaultUri),
new ChainedTokenCredential(
new AzureCliCredential(),
new ManagedIdentityCredential(),
new DefaultAzureCredential()
),
new AzureKeyVaultConfigurationOptions()
{
ReloadInterval = TimeSpan.FromMinutes(30)
});
})
.ConfigureAppConfiguration((_, builder) =>
{
builder.AddJsonFile("appsettings.local.json", optional: true);
})

From there, you can redefine whatever secrets you might want to use, as long as they have the same name. The last registered setting will win over the previously declared version.