Last active
June 8, 2019 09:06
-
-
Save kasunkv/c363d5f8c89b3b9ee3fb21b781833c00 to your computer and use it in GitHub Desktop.
Using AzureServiceTokenProvider to fetch the Access Token for Key Vault
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
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