【问题标题】:Azure B2C - Failed to acquire token silentlyAzure B2C - 无法以静默方式获取令牌
【发布时间】:2016-08-12 06:58:02
【问题描述】:

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

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

静默获取令牌失败。调用方法AcquireToken text --> Code

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

    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));
    }

【问题讨论】:

    标签: azure adal azure-ad-b2c


    【解决方案1】:

    您的缓存是空的,因为它没有在任何地方持久化。查看http://www.cloudidentity.com/blog/2014/07/09/the-new-token-cache-in-adal-v2/。搜索 EFADALTokenCache,你会找到可以帮助你将缓存持久化到某个存储的实现。

    仅通过https://www.nuget.org/packages/Microsoft.Identity.Client 提供的名为 MSAL 的新库支持 Azure B2C。此库仍处于预览阶段。

    【讨论】:

    猜你喜欢
    • 2016-07-12
    • 2017-10-08
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多