Azure Key Vault
Azure Key Vault is a tool for storing and retrieving secrets in a secure way in cloud. Example of secrets: API keys, Passwords,
or Certificates.
Access Management
This service allows you to manage not only your keys but also those who have access to them. You can grant granular
permissions to each key to only the users and applications who need access.
Access Monitoring
Monitoring for compliance and audit is another crucial component to key management. Azure Key Vault also provides
logging into what and who accesses what is in your vault. By enabling logging for Key Vault, it saves data in an Azure storage
account you create and stores all the information in needs for reporting within a retention range you set
Steps:
1. Select Azure Active Directory and then click on App registrations
2.Click New registration link
Click on New client secret
Create Key Valult
View all resources and click on Create New Resource, then search Key vault
Click on Secrets
Click on Access Policies link to proceed
Click on Add Access Policy
Select Secret permissions, select our app which we have created previously , click on ‘None selected’ for Select Principal
field
Create a Web API project
appSettings.json
"KeyVault": {
"Vault": "logixnvault",
"ClientId": "a2d9cfbb-bae2-4da9-ad3c-24ad38948f38",
"ClientSecret": "dFiFZI~-15wAE-fN1tjl-9n4vW38be0qm-"
}
Add nugget package Microsoft.Extensions.Configuration.AzureKeyVault
Change in Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var root = config.Build();
config.AddAzureKeyVault($"https://{root["KeyVault:Vault"]}.vault.azure.net/",
root["KeyVault:ClientId"], root["KeyVault:ClientSecret"]);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
Add a controller and below code
private readonly IConfiguration _configuration;
public ValuesController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet]
public string Get()
{
var value = _configuration["FirstSecret"];
return "Value for Secret [FirstSecret] is : " + value;
}