Created
November 4, 2018 02:03
-
-
Save frankhu-2021/e021d69404c397348f2070f2a091f41f to your computer and use it in GitHub Desktop.
Get the access token using ADAL .net library using an interactive login flow
This file contains hidden or 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
using Microsoft.IdentityModel.Clients.ActiveDirectory; | |
static async Task getAccessToken() | |
{ | |
int retryCount = 0; | |
bool retry = false; | |
authContext = new AuthenticationContext(authority + tenantID); | |
do | |
{ | |
retry = false; | |
try | |
{ | |
// ADAL includes an in memory cache, so this call will only send a message to the server if the cached token is expired. | |
result = await authContext.AcquireTokenAsync(resourceUri, clientId, new Uri(redirectUri), new PlatformParameters(PromptBehavior.Auto)); | |
Console.Write("My Access Token : \n"); | |
await prettyJWTPrint(result.AccessToken); | |
} | |
catch (AdalException ex) | |
{ | |
if (ex.ErrorCode == "temporarily_unavailable") | |
{ | |
retry = true; | |
retryCount++; | |
Thread.Sleep(3000); | |
} | |
Console.WriteLine( | |
String.Format("An error occurred while acquiring a token\nTime: {0}\nError: {1}\nRetry: {2}\n", | |
DateTime.Now.ToString(), | |
ex.ToString(), | |
retry.ToString())); | |
} | |
} while ((retry == true) && (retryCount < 3)); | |
if (result == null) | |
{ | |
Console.WriteLine("Canceling attempt to get access token.\n"); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment