Created
March 3, 2017 04:15
-
-
Save jlattimer/ce0235d53a5c19368949463181513397 to your computer and use it in GitHub Desktop.
This file contains 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
string secretUrl = "https://myvaulttest.vault.azure.net/secrets/MyPassword/00000000000000000000000000000000"; | |
//Retrieve a secret by its url | |
var getKeyByUrlTask1 = Task.Run(async () => await GetSecretByUrl(token, secretUrl)); | |
Task.WaitAll(getKeyByUrlTask1); | |
if (getKeyByUrlTask1.Result == null) | |
throw new InvalidPluginExecutionException("Error retrieving secret value from key vault"); | |
//Deserialize the vault response to get the secret | |
GetSecretResponse getSecretResponse1 = DeserializeResponse<GetSecretResponse>(getKeyByUrlTask1.Result); | |
//returnedValue is the Azure Key Vault Secret | |
string returnedValue = getSecretResponse1.value; | |
//Get the Secret value from the Key Vault by url - api-version is required | |
private async Task<string> GetSecretByUrl(string token, string secretUrl) | |
{ | |
using (HttpClient httpClient = new HttpClient()) | |
{ | |
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, | |
new Uri(secretUrl + "?api-version=2016-10-01")); | |
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); | |
HttpResponseMessage response = await httpClient.SendAsync(request); | |
return !response.IsSuccessStatusCode ? null | |
: response.Content.ReadAsStringAsync().Result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment