Last active
June 24, 2021 08:14
-
-
Save emreozguruoglu/26afc7754292a3fd590939532c965c8b 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
var options = new PublicClientApplicationOptions | |
{ | |
ClientId = "ClientId", | |
TenantId = "TenantId", | |
RedirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient", | |
AzureCloudInstance = AzureCloudInstance.AzurePublic | |
}; | |
var publicClientApplication = PublicClientApplicationBuilder.CreateWithApplicationOptions(options).Build(); | |
//TokenCacheHelper.EnableSerialization(publicClientApplication.UserTokenCache); | |
var accounts = await publicClientApplication.GetAccountsAsync(); | |
var scopes = new string[] { | |
"email", | |
"offline_access", | |
"https://outlook.office.com/IMAP.AccessAsUser.All", // Only needed for IMAP | |
//"https://outlook.office.com/POP.AccessAsUser.All", // Only needed for POP | |
//"https://outlook.office.com/SMTP.AccessAsUser.All", // Only needed for SMTP | |
}; | |
var firstAccount = accounts.FirstOrDefault(); | |
AuthenticationResult authToken = null; | |
try | |
{ | |
authToken = await publicClientApplication.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync(); | |
} | |
catch (MsalUiRequiredException) | |
{ | |
try | |
{ | |
authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync(); // HANGING AFTER FIRST SUCCESS CONNECTION | |
} | |
catch (MsalException msalex) | |
{ | |
Console.WriteLine("Error Acquiring Token:" + msalex.Message); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
//var authToken = await publicClientApplication.AcquireTokenSilent(scopes, firstAccount).ExecuteAsync(); | |
var oauth2 = new SaslMechanismOAuth2(authToken.Account.Username, authToken.AccessToken); | |
await client.DisconnectAsync(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment