I'm developing a desktop app.
It is already created without OAuth 2.0 we have used Imap.UseBestLogin
before. Our goal is to connect with Office365 and OAuth 2.0 (Imap.LoginOAUTH2
) to maintain the desktop app.
This is what I tried:
// LIMILABS SUPPORT WARNING: THIS CODE IS INVALID - DO NOT USE
string clientId = "client id";
string email = "testmail@contoso.com";
var app = PublicClientApplicationBuilder
.Create(clientId)
//.WithTenantId(tenantId)
.WithAuthority(
AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount)
.WithDefaultRedirectUri()
.Build();
// This allows saving access/refresh tokens to some storage
TokenCacheHelper.EnableSerialization(app.UserTokenCache);
var scopes = new string[]
{
"offline_access",
"email",
"https://outlook.office.com/IMAP.AccessAsUser.All",
"https://outlook.office.com/POP.AccessAsUser.All",
"https://outlook.office.com/SMTP.Send",
};
string userName;
string accessToken;
var account = app.GetAccountsAsync().Result.FirstOrDefault();
try
{
AuthenticationResult refresh = app
.AcquireTokenSilent(scopes, account)
.ExecuteAsync().Result;
userName = refresh.Account.Username;
accessToken = refresh.AccessToken;
}
//catch (MsalUiRequiredException ex)
catch (AggregateException ex)
{
var result = app.AcquireTokenSilent(scopes, email)
.ExecuteAsync().Result;
userName = result.Account.Username;
accessToken = result.AccessToken;
}
// LIMILABS SUPPORT WARNING: THIS CODE IS INVALID - DO NOT USE
even I use await still I cannot get a token. or the issue is in the azure ad?