Skip to content

Instantly share code, notes, and snippets.

@khle
Created August 26, 2021 05:11
Show Gist options
  • Save khle/ebaa85f044aa92647573fd3d7637651c to your computer and use it in GitHub Desktop.
Save khle/ebaa85f044aa92647573fd3d7637651c to your computer and use it in GitHub Desktop.
Startup.cs
//Other using import statements
//We only import 2 packages
using Azure.Security.KeyVault.Secrets;
using Azure.Identity;
//Other codes remain unchanged
//Only changes to be made are in this method ConfigureServices()
public void ConfigureServices(IServiceCollection services)
{
var vaultName = "MyStageAzureSecretVault";
var keyVaultUrl = $"https://{vaultName}.vault.azure.net";
var client = new SecretClient(vaultUri: new Uri(keyVaultUrl), credential: new DefaultAzureCredential());
var secretsDict = new string [] {"DatabaseUserId", "DatabasePassword"}
.Select(async name => await client.GetSecretAsync(name))
.Select(task => task.Result.Value)
.ToDictionary(secret => secret.Name, secret => secret.Value);
//Optinally write to console to confirm
foreach(var name in new string [] {"DatabaseUserId", "DatabasePassword"})
{
Console.Out.WriteLine($"{name}: {secretsDict[name]}");
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment