Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlattimer/ce0235d53a5c19368949463181513397 to your computer and use it in GitHub Desktop.
Save jlattimer/ce0235d53a5c19368949463181513397 to your computer and use it in GitHub Desktop.
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