Skip to content

Instantly share code, notes, and snippets.

@oleksabor
Created April 23, 2018 07:44
Show Gist options
  • Save oleksabor/94c8c580441c6061d459758811ddc27e to your computer and use it in GitHub Desktop.
Save oleksabor/94c8c580441c6061d459758811ddc27e to your computer and use it in GitHub Desktop.
Azure Vault key access sample using applicationid and secret. It is slightly modified MS sample
using Microsoft.Azure.KeyVault;
using Microsoft.Azure.KeyVault.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace azureKeyV
{
class Program
{
static void Main(string[] args)
{
var clientId = "GUID value";
var secret = "secret (Base64 encoded string)";
var clientCred = new ClientCredential(clientId, secret);
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
(authority, resource, scope) => GetAccessToken(authority, resource, scope, clientCred)));
var vaultAddress = "https://something.vault.azure.net";
var keyName = "keyName here";
//enumerates all secrets in the vault
var secrets = keyVaultClient.GetSecretsAsync(vaultAddress).ConfigureAwait(false).GetAwaiter().GetResult();
foreach (var s in secrets)
Console.WriteLine("secret {0}", s.Identifier);
//enumerates all keys in the vault
var keys = keyVaultClient.GetKeysAsync(vaultAddress).ConfigureAwait(false).GetAwaiter().GetResult();
foreach (var k in keys)
Console.WriteLine("key {0}", k.Identifier);
var retrievedKey = Task.Run(() => keyVaultClient.GetKeyAsync(vaultAddress, keyName)).ConfigureAwait(false).GetAwaiter().GetResult();
PrintoutKey(retrievedKey);
Console.WriteLine(Convert.ToBase64String(retrievedKey.Key.N));
Console.ReadKey();
}
/// <summary>
/// Gets the access token
/// </summary>
/// <param name="authority"> Authority </param>
/// <param name="resource"> Resource </param>
/// <param name="scope"> scope </param>
/// <returns> token </returns>
public static async Task<string> GetAccessToken(string authority, string resource, string scope, ClientCredential cred)
{
var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
var result = await context.AcquireTokenAsync(resource, cred).ConfigureAwait(false);
return result.AccessToken;
}
private static void PrintoutKey(SecretBundle value)
{
Console.Out.WriteLine("Key: \n\tKey ID: {0}\n\tKey type: {1}",
value.SecretIdentifier, value.Kid);
}
private static void PrintoutKey(KeyBundle keyBundle)
{
Console.Out.WriteLine("Key: \n\tKey ID: {0}\n\tKey type: {1}",
keyBundle.Key.Kid, keyBundle.Key.Kty);
var expiryDateStr = keyBundle.Attributes.Expires.HasValue
? keyBundle.Attributes.Expires.ToString()
: "Never";
var notBeforeStr = keyBundle.Attributes.NotBefore.HasValue
? keyBundle.Attributes.NotBefore.ToString()
: UnixTimeJsonConverter.EpochDate.ToString();
Console.Out.WriteLine("Key attributes: \n\tIs the key enabled: {0}\n\tExpiry date: {1}\n\tEnable date: {2}",
keyBundle.Attributes.Enabled, expiryDateStr, notBeforeStr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment