Skip to content

Instantly share code, notes, and snippets.

@kasunkv
Last active June 8, 2019 09:06
Show Gist options
  • Save kasunkv/c363d5f8c89b3b9ee3fb21b781833c00 to your computer and use it in GitHub Desktop.
Save kasunkv/c363d5f8c89b3b9ee3fb21b781833c00 to your computer and use it in GitHub Desktop.
Using AzureServiceTokenProvider to fetch the Access Token for Key Vault
public async Task<IActionResult> Index()
{
var vaultName = _configuration["AppSettings:KeyVaultName"];
var secretName = _configuration["AppSettings:SecretName"];
var vm = new HomeViewModel();
try
{
// Creating the Key Vault client
var tokenProvider = new AzureServiceTokenProvider();
var vaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback));
// Get the secret
var secret = await vaultClient.GetSecretAsync($"https://{vaultName}.vault.azure.net/secrets/{secretName}");
vm.SecretValue = secret.Value;
}
catch (Exception ex)
{
vm.IsError = true;
vm.ErrorMessage = ex.Message;
}
return View(vm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment