【发布时间】:2019-04-12 14:41:34
【问题描述】:
我们有一个 Web 应用程序,它需要对多个 Web API 进行身份验证访问。我们正在使用 Azure AD B2C 进行身份验证,除了一个非常令人沮丧且非常有限的问题外,我们工作正常:我们一次只能取回一个有效的访问令牌,但我们需要多个。
下面的 sn-p 显示了关注的区域。
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
{
await GetAccessToken(notification, TaskScopes);
await GetAccessToken(notification, UserScopes);
}
private async Task<AuthenticationResult> GetAccessToken(AuthorizationCodeReceivedNotification notification, string[] scopes)
{
string signedInUserID = notification.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
TokenCache userTokenCache = new MSALSessionCache(signedInUserID, notification.OwinContext.Environment["System.Web.HttpContextBase"] as HttpContextBase).GetMsalCacheInstance();
ConfidentialClientApplication cca = new ConfidentialClientApplication(ClientId, Authority, RedirectUri, new ClientCredential(ClientSecret), userTokenCache, null);
try
{
return await cca.AcquireTokenByAuthorizationCodeAsync(notification.Code, scopes);
}
catch (Exception ex)
{
//TODO: Handle
throw;
}
}
显示的代码 sn-ps 在从 Azure AD B2C 接收到身份验证令牌之后调用。。如图所示执行此代码时,将返回两个访问令牌中的第一个,并且完全有效。但是,第二个具有 access_token: null 和 first 令牌的范围。
此外,如果我们简单地注释掉对GetAccessToken 的两个调用中的任何一个,则剩余的调用会按预期工作,无论它是两个调用中的哪一个。这似乎表明我们所有的配置都是正确的,因此我不会在此处发布这些配置以供审核。
我见过的所有示例代码都只显示了一个正在获取的访问令牌。同样,当我们隔离这两个调用中的任何一个时,它都能完美运行。只有当我们尝试获取两个令牌时它才会失败,并且总是在两个令牌中的第二个,无论顺序如何。此外,发生这种情况时,我们没有遇到任何异常。
谁能提供关于这里可能发生的事情的任何线索?感谢您的帮助。
【问题讨论】:
标签: authentication azure-active-directory access-token azure-ad-b2c