Created
January 17, 2020 14:31
-
-
Save pmatthews05/f5ebee4a3f66ed3b2fbb2989a32c02a3 to your computer and use it in GitHub Desktop.
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
internal static async Task<string> AcquireTokenForApplication() | |
{ | |
var tenant = System.Environment.GetEnvironmentVariable("Tenant", EnvironmentVariableTarget.Process); | |
var clientId = System.Environment.GetEnvironmentVariable("ClientId", EnvironmentVariableTarget.Process); | |
var secret = System.Environment.GetEnvironmentVariable("Secret", EnvironmentVariableTarget.Process); | |
var authorityUri = $"https://login.microsoftonline.com/{tenant}.onmicrosoft.com"; | |
var resourceUri = "https://manage.office.com"; | |
var microsoftToken = await GetTokenRetry(resourceUri, authorityUri, clientId, secret); | |
return microsoftToken; | |
} | |
private static async Task<string> GetTokenRetry(string resourceUri, string authorityUri, string clientId, string secret, int retryCount = 5, int delay = 500) | |
{ | |
... | |
AuthenticationContext authContext = new AuthenticationContext(authorityUri, false); | |
ClientCredential clientCred = new ClientCredential(clientId, secret); | |
var authenticationResult = await authContext.AcquireTokenAsync(resourceUri, clientCred); | |
token = authenticationResult.AccessToken; | |
return token; | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment