I've some .NET 9 applications that use appsettings.json to load settings, but some of them contain sensitive data, and I would like to resolve them as I do for Azure functions via @Microsoft.KeyVault directive.
I've found this link, but it applies to web.config, I don't know how and if I've to set the binder as it's told.
My settings are the following:
"KeyVaultUrl": "https://mykeyvault-dev.vault.azure.net/",
"AzureConfig": {
"Url": "https://myappconfig.azconfig.io",
"RefreshInterval": "00:02:00"
},
"ElasticPassword":"@Microsoft.KeyVault(VaultName=mykeyvault-dev;SecretName=elastic-search-password)",
Now, if this is in Azure Functions, the correct value from the vault is resolved automatically even when I run from Visual Studio inside, when using
var item = _configuration.GetValue<string>("something");
In a web application (I would say the one that has var builder=WebApplication.CreateBuilder()), it does not seem to work.
The code I'm trying to use is
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Get Key Vault details
var configurationTmp = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var tmp = configurationTmp.Build();
string appConfigurationUrl = tmp.GetValue<string>("RedactionConfig:AzureConfig:Url");
TimeSpan refreshTimespan = tmp.GetValue<TimeSpan>("RedactionConfig:AzureConfig:RefreshInterval");
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddAzureKeyVault(
new Uri("https://myVault.azure.net/"),
new DefaultAzureCredential(),
new AzureKeyVaultConfigurationOptions
{
ReloadInterval = TimeSpan.FromMinutes(5)
})
.AddEnvironmentVariables()
.AddAzureAppConfiguration(delegate (AzureAppConfigurationOptions opt)
{
var tokenCredential = new DefaultAzureCredential();
opt.Connect(new Uri(appConfigurationUrl), tokenCredential).ConfigureKeyVault(
delegate (AzureAppConfigurationKeyVaultOptions kv)
{
kv.SetCredential(tokenCredential);
});
})
.Build();
builder.Configuration.AddConfiguration(configuration);
}
When I try to resolve, I get as value
@Microsoft.KeyVault(VaultName=kv-as-shared-dev;SecretName=elastic-search-password)
Can anyone help me with this?
Thanks





