Skip to content

Instantly share code, notes, and snippets.

@bayological
Created July 8, 2019 23:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Gets auth token
private static async Task<TokenCredentials> GetTokenCredentialsAsync()
{
var clientCredential = new ClientCredential(_clientId, _clientSecret);
var authContext = new AuthenticationContext($"https://login.windows.net/{_tenantId}");
AuthenticationResult result;
try
{
result = await authContext.AcquireTokenAsync("https://management.azure.com/", clientCredential).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine($"Could not get access token for management api. Exception: {ex.Message}");
throw ex;
}
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
TokenCredentials Cred = new TokenCredentials(result.AccessToken);
return Cred;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment