Created
July 8, 2019 23:02
-
-
Save bayological/98796362d8e8e65064e18f944d4267df to your computer and use it in GitHub Desktop.
Gets auth token
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
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