Created
August 26, 2021 05:11
-
-
Save khle/ebaa85f044aa92647573fd3d7637651c to your computer and use it in GitHub Desktop.
Startup.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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