Attempting to retrieve secrets from KeyVault in a C# App Service.
Local machine:
Visual Studio > Tools > Options > Azure Service Authentication - authenticated Azure account
Likely use
az login
in the shell that youdotnet run
if on vs code etc. Not Checked.
Azure
- App service blade:
- Set App Service identity to
System Assigned
- Set App Service identity to
- Keyvault blade:
- Created KeyVault
- Created Secret: Name = "Foo"
- Given myself manage secrets access policy
- Given App Service identity Get and List secret access policy
appsettings.json
...
"KeyVaultName" : "abc123",
"Secrets": {
"One" : "@Microsoft.KeyVault(Secreturi=[uri to secret copied from Azure blade])"
}
...
Program.cs
...
using Azure.Extensions.AspNetCore.Configuration.Secrets;
using Azure.Identity;
...
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var builtConfig = config.Build();
var secretClient = new SecretClient(
new Uri($"https://{builtConfig["KeyVaultName"]}.vault.azure.net/"),
new DefaultAzureCredential());
config.AddAzureKeyVault(secretClient, new KeyVaultSecretManager());
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Result
I am just getting the @Microsoft ...
value which I had expected to be mapped to the value from the keyvault.
Something seems off as I have to define the name of the keyvault twice, once in the SecretClient and once in the @Microsoft.KeyVault reference.