Skip to content

Instantly share code, notes, and snippets.

@bayological
Created July 8, 2019 23:02
Show Gist options
  • Save bayological/98796362d8e8e65064e18f944d4267df to your computer and use it in GitHub Desktop.
Save bayological/98796362d8e8e65064e18f944d4267df to your computer and use it in GitHub Desktop.
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