Azure B2C - 无法以静默方式获取令牌

时间:2016-04-19 21:11:20

标签: azure adal azure-ad-b2c

我正在使用此模板使用ASP.NET MVC和WebAPI构建应用程序:Azure AD B2C WebApp / WepAPI。我通过web.config文件配置了我的Azure B2C AD,当我点击“登录”时,我会看到我的身份提供商。到目前为止登录工作(我在右上角看到我的用户名),我能够执行“待办事项列表” - 动作。

但是当我停止调试器并按F5重新启动应用程序时,当我再次单击“待办事项列表” - 动作时出现错误。

无法以静默方式获取令牌。调用方法AcquireToken文本 - > Code

发生这种情况,导致用户仍然经过身份验证,但应用程序重新启动后NaiveSessionCache为空。一个可能的解决方案是,将令牌存储在OnAuthorizationCodeReceived Handler中,但我看起来有点奇怪

    private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedNotification notification)
    {
        string userObjectID = notification.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
        string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant, string.Empty, string.Empty);
        ClientCredential credential = new ClientCredential(clientId, clientSecret);

        string mostRecentPolicy = notification.AuthenticationTicket.Identity.FindFirst(Startup.AcrClaimType).Value;
        AuthenticationContext authContext = new AuthenticationContext(authority);

        AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(notification.Code, new Uri(redirectUri), credential, new string[] { clientId }, mostRecentPolicy);

        // Store token in ClaimsIdentity
        notification.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim("Token", result.Token));
    }

1 个答案:

答案 0 :(得分:3)

您的缓存为空,因为它没有在任何地方保留。查看enter image description here。搜索EFADALTokenCache,您将找到有助于将缓存保存到某个存储的实现。

只有http://www.cloudidentity.com/blog/2014/07/09/the-new-token-cache-in-adal-v2/提供的名为MSAL的新库才支持Azure B2C。该库仍处于预览状态。

相关问题