4

我正在使用配置了一个身份提供程序 (LinkedIn) 的 Azure B2C。我有一个 Web API (b2c bearer auth) 和一个 Web App MVC (b2c Open Id)。

我正在尝试创建一个持久登录 - 这意味着用户可以每 90 天从给定的设备+浏览器通过 LinkedIn 登录一次。

我得到的最接近的是当我在 Web 应用程序中添加 IsPersistent = true 代码以启用它时:

更新:更新了基于 Azure B2C GA 的代码。为了达到我在 Preview 中的位置,我仍然使用自定义授权属性,但代码已更新:

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
          filterContext.HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties()
            {
                IsPersistent = true
            });
        base.HandleUnauthorizedRequest(filterContext);
    }

但是,这仅在大约 1 小时内有效。也许它遵循访问和 ID 政策?刷新令牌没有限制 - 我不确定为什么“IsPersistent”只有 1 小时。

Azure 中的令牌会话配置

所以这导致了这些问题:

  1. 我可以使用 Azure B2C (OpenId Connect) 实现持久会话(60-90 天)吗?
  2. 如果是这样,关于我缺少什么的任何指示?我需要做一些自定义 cookie 验证吗?带有刷新令牌的东西(我将它们用于 web api,但在 web 应用程序中没有自定义)。

任何想法或输入都会很棒!

4

1 回答 1

2

在执行以下操作后,我已经能够实现与 B2C 的持久会话:

  1. 自定义授权属性

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        filterContext.HttpContext.GetOwinContext()
             .Authentication.Challenge(
                  new AuthenticationProperties() { IsPersistent = true }
              );
        base.HandleUnauthorizedRequest(filterContext);
    }
    
  2. 使用 Microsoft.Experimental.IdentityModel.Clients.ActiveDirectory 而不是 BootstrapContext (基本上与 pre-GA 代码示例一起使用(查看更改历史记录)-> https://github.com/AzureADQuickStarts/B2C-WebApp-WebAPI-OpenIDConnect-DotNet) . ADAL 库处理获取对我的代码透明的适当令牌。

  3. 实现了自定义 TokenCache(基于此处的 EFADAL 示例:https ://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-multitenant-openidconnect/blob/master/TodoListWebApp/DAL/EFADALTokenCache.cs )

  4. 更改了 Startup.Auth.cs:

    return new OpenIdConnectAuthenticationOptions
    {
        MetadataAddress = String.Format(aadInstance, tenant, policy),
        AuthenticationType = policy,
        UseTokenLifetime = false,
        ClientId = clientId,
        RedirectUri = redirectUri,
        PostLogoutRedirectUri = redirectUri,
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
    
            AuthenticationFailed = OnAuthenticationFailed,
            AuthorizationCodeReceived = OnAuthorizationCodeReceived,
        },
        Scope = "openid offline_access",
        ResponseType = "code id_token",
    
        TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = "name",
            SaveSigninToken = true,
    
        },
    }
    
于 2016-08-15T22:59:31.340 回答