Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Created August 8, 2017 01:18
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 sjwaight/8d4751e45f73c37300c2571c2e8a2a40 to your computer and use it in GitHub Desktop.
Save sjwaight/8d4751e45f73c37300c2571c2e8a2a40 to your computer and use it in GitHub Desktop.
Sample Azure Key Vault Service Principal Auth Token helper class.
namespace MyDemo.Website.WebApi.Helpers
{
public static class KevVaultUtils
{
// You could also utilise a certificate-based service principal as well
// Yes, these are hardcoded :)
private const string kvSP = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
private const string kvSPkey = "NOTREALLYTHEPASSWORD";
public static async Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
var clientCred = new ClientCredential(kvSP, kvSPkey);
var result = await authContext.AcquireTokenAsync(resource, clientCred);
if (result == null)
throw new InvalidOperationException("Failed to obtain the JWT token");
return result.AccessToken;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment