【问题标题】:Thinktecture Identity v2 rolling session security tokenThinktecture Identity v2 滚动会话安全令牌
【发布时间】:2015-01-08 05:08:12
【问题描述】:

我目前正在将 Identity 用于登录系统,但它创建的会话令牌提供了 10 小时的固定到期日期。如果用户空闲 20 分钟,我的系统的规范要求会话到期。我在源代码中找不到提供滚动会话状态的任何地方。

我已经用谷歌搜索了这个问题,唯一的解决方案是每次在 global.asax 中引发 SessionAuthenticationModule_SessionSecurityTokenReceived 事件时从 sessionAuthenticationModule 创建一个新会话。

        if (validFrom.AddMinutes(halfSpan) < now && now < validTo)
        {
            var sam = sender as SessionAuthenticationModule;

            e.SessionToken = sam.CreateSessionSecurityToken(
                e.SessionToken.ClaimsPrincipal,
                e.SessionToken.Context,
                now,
                now.AddMinutes(5),
                e.SessionToken.IsPersistent);
            e.ReissueCookie = true;
        }

这种方法有更好的替代方法吗?

【问题讨论】:

    标签: c# asp.net identity


    【解决方案1】:

    ThinkTecture 的成员 Allen Brock 建议如果会话仍然有效但超过一半过期,我们重新颁发令牌:

    void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
    {
        SessionAuthenticationModule sam = FederatedAuthentication.SessionAuthenticationModule;
    
        var token = e.SessionToken;
        var duration = token.ValidTo.Subtract(token.ValidFrom);
        if (duration <= TimeSpan.Zero) return;
    
        var diff = token.ValidTo.Add(sam.FederationConfiguration.IdentityConfiguration.MaxClockSkew).Subtract(DateTime.UtcNow);
        if (diff <= TimeSpan.Zero) return;
    
        var halfWay = duration.TotalMinutes / 2;
        var timeLeft = diff.TotalMinutes;
        if (timeLeft <= halfWay)
        {
            e.ReissueCookie = true;
            e.SessionToken =
                new SessionSecurityToken(
                    token.ClaimsPrincipal,
                    token.Context,
                    DateTime.UtcNow,
                    DateTime.UtcNow.Add(duration))
                {
                    IsPersistent = token.IsPersistent,
                    IsReferenceMode = token.IsReferenceMode
                };
        }
    }
    

    如果你同意,你不需要自己写,可以通过global.asax调用:

    public override void Init()
    {
        PassiveModuleConfiguration.EnableSlidingSessionExpirations();
    }
    

    来源:http://brockallen.com/2013/02/17/sliding-sessions-in-wif-with-the-session-authentication-module-sam-and-thinktecture-identitymodel/

    另请参阅Updating BootStrapContext with new SessionSecurityToken when using Sliding sessions in WIF with the SAM and Thinktecture IdentityModel 以了解与此相关的问题:序列化为当前声明的 BootStrapToken 身份仍然是旧的。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2020-04-17
      • 2014-06-16
      • 2014-02-01
      • 2019-12-09
      相关资源
      最近更新 更多