But this code will not run in local, i.e, while development. For this we need to install Azure CLI (azure-cli-2.0.29.msi) to make use of MSI in local environment. After installing this open Microsoft azure command prompt and run
"az login" command and open the url mentioned in the command prompt and copy the code mentioned in prompt in that url. Now you would be able to make use of key vault using MSI in local and app service as well.
Dictionary<string, string> secretlist = new Dictionary<string, string>();
AzureServiceTokenProvider azureServiceTokenProvider = new AzureServiceTokenProvider();
// TO get access token to azureServices
Task<string> accessToken = azureServiceTokenProvider.GetAccessTokenAsync("https://vault.azure.net");
accessToken.Wait();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var all = keyVaultClient.GetSecretsAsync("https://keyvaultName.vault.azure.net/");
string seperator = "secrets/";
foreach (Microsoft.Azure.KeyVault.Models.SecretItem someItem in all.Result)
{
var secretName = someItem.Identifier;
var secretValue = keyVaultClient.GetSecretAsync(secretName.ToString());
secretValue.Wait();
secretlist.Add(secretName.ToString().Substring(secretName.ToString().IndexOf(seperator) + seperator.Length), secretValue.Result.Value);
}