Created
August 8, 2017 01:18
-
-
Save sjwaight/8d4751e45f73c37300c2571c2e8a2a40 to your computer and use it in GitHub Desktop.
Sample Azure Key Vault Service Principal Auth Token helper class.
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
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