提问人:Fabian Saam 提问时间:11/7/2023 更新时间:11/7/2023 访问量:62
C# WinForms - 这会导致无限循环:await publicClientApplication.AcquireTokenInteractive(scopes)。ExecuteAsync();
C# WinForms - This causes a endless loop: await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
问:
整体功能:
public static async Task<SaslMechanismOAuth2> AuthenticateMS365Async(Benutzer user)
{
if (!user.UseImapM365 && !user.UsePopM365)
return null;
SaslMechanismOAuth2 oauth2 = null;
string[] scopes = new string[] { };
if (user.UseImapM365)
{
scopes = new string[]
{
"email",
"offline_access",
"https://outlook.office.com/IMAP.AccessAsUser.All", // Only needed for IMAP
};
}
else if (user.UsePopM365)
{
scopes = new string[]
{
"email",
"offline_access",
"https://outlook.office.com/POP.AccessAsUser.All", // Only needed for POP
};
}
var options = new PublicClientApplicationOptions
{
ClientId = user.MS_CLIENTID,
TenantId = user.MS_TENANTID,
RedirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
};
var storageProperties = new StorageCreationPropertiesBuilder("merlin_msal_cache.dat", MsalCacheHelper.UserRootDirectory).Build();
var publicClientApplication = PublicClientApplicationBuilder
.CreateWithApplicationOptions(options)
.Build();
var cacheHelper = await MsalCacheHelper.CreateAsync(storageProperties);
cacheHelper.RegisterCache(publicClientApplication.UserTokenCache);
AuthenticationResult authToken;
try
{
authToken = await publicClientApplication.AcquireTokenSilent(scopes, EMailService.MS_AuthAccount).ExecuteAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//Console.WriteLine(ex.StackTrace);
authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
var accounts = await publicClientApplication.GetAccountsAsync().ConfigureAwait(true);
EMailService.MS_AuthAccount = accounts.FirstOrDefault();
}
oauth2 = new SaslMechanismOAuth2(authToken.Account.Username, authToken.AccessToken);
return oauth2;
}
此功能有效。我在程序的一部分中测试了它,但不知何故在程序的另一部分以无限循环结束(卡住了)。
authToken = await publicClientApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
这是它陷入无限循环的部分。
我真的不知道它可能是什么。
AcquireTokenInteractive 在程序的不同部分中同样工作并且不会显示不稳定的行为?
我多次检查输入,它完全相同(客户端 ID 和租户 ID 等)。
我现在一无所知......
答: 暂无答案
下一个:将键对值添加到缺少的字典中
评论