【发布时间】:2020-12-09 02:31:57
【问题描述】:
我正在尝试使用 MSAL.NET 获取令牌,并且几乎使用了他们的开箱即用教程代码。
using Microsoft.Identity.Client;
using MyApp.Interfaces;
using System;
using System.Threading.Tasks;
namespace MyApp.NetworkServices
{
public class MyAuthorizationClient : IMyAuthorizationClient
{
private readonly string[] _resourceIds;
private IConfidentialClientApplication App;
public MyAuthorizationClient(IMyAuthenticationConfig MyAuthenticationConfig)
{
_resourceIds = new string[] { MyAuthenticationConfig.MyApimResourceID };
App = ConfidentialClientApplicationBuilder.Create(MyAuthenticationConfig.MyApimClientID)
.WithClientSecret(MyAuthenticationConfig.MyApimClientSecret)
.WithAuthority(new Uri(MyAuthenticationConfig.Authority))
.Build();
}
public async Task<AuthenticationResult> GetMyAccessTokenResultAsync()
{
AuthenticationResult result = null;
try
{
result = await App.AcquireTokenForClient(_resourceIds).ExecuteAsync().ConfigureAwait(continueOnCapturedContext:false);
}
catch(MsalClientException ex)
{
...
}
return result;
}
}
}
我遇到的问题是在await 调用中,它永远不会返回。调试器不会恢复控制,应用程序会进入前台,就好像它继续运行一样。我无法查询result 的结果,并且我已经将await 配置为不继续。
我查看了这个很棒的帖子,但没有为我的场景找到任何解决方案:Async call with await in HttpClient never returns
【问题讨论】:
-
我正在努力解决完全相同的问题。我能够在 ApplicationBuilder 上使用 WithLogging() 并输出到文件以至少获取一些日志记录,但它总是在“从主机 login.microsoftonline.com 的网络中获取实例发现”时死掉。无法弄清楚实际的根本原因是什么,但也许这将有助于您进一步诊断您的问题。
-
通过使用 .ExecuteAsync().Result 没有等待,我能够让它抛出异常而不是使调试器崩溃,这导致它实际评估并给我一些回报。似乎我的范围是错误的,但不知道为什么。
标签: c# .net concurrency async-await msal