【问题标题】:Accessing cookie expiration time owin访问 cookie 过期时间
【发布时间】:2016-02-23 03:35:35
【问题描述】:

我正在尝试访问 Owin 上的过期时间,我正在使用以下示例

Access ExpireTimeSpan property of Owin Cookie Authentication to notify user of login expiry

但我不能上班。我得到一个错误的键,值确实存在。如果有人能指出正确的方向,我将不胜感激。

StartupAuth.cs

     var config1 = new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            //LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))

            }
        };

        app.UseCookieAuthentication(config1);
        var options = new CookieAuthenticationOptions
        {
            // usual options such as LoginPath, for example, go here...
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = context =>
                {
                    DateTimeOffset now = DateTimeOffset.UtcNow;

                    context.OwinContext.Request.Set<double>("time.Remaining",
                           context.Properties.ExpiresUtc.Value.Subtract(now).TotalSeconds);

                    return Task.FromResult<object>(null);
                }
            }
        };
        app.UseCookieAuthentication(options);

控制器

  [Authorize]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        //var secondsRemaining = (double)Request.GetOwinContext()
        //                            .Environment["time.Remaining"];
       return View();
    }

}

【问题讨论】:

    标签: c# asp.net-mvc cookies session-cookies owin


    【解决方案1】:

    这个答案有另一种存储值的方法。它将其存储为声明。你可以试试这个:How to know when OWIN cookie will expire?

    【讨论】:

    • ApplicationValidateIdentity 如果使用SlidingExpiration = true,则在更新cookie之前执行
    【解决方案2】:

    这是可行的,但有点难看.. 它解释了SlidingExpiration 逻辑.. 请注意,在我的情况下,登录请求中的声明为空,所以我将其设置为sessionTimeInMinutes

    SlidingExpiration = true,
    CookieName = applicationCookieName,
    Provider = new CookieAuthenticationProvider
    {
        // Not called on signin
        OnValidateIdentity = context =>
        {
            // this is the last value before ExpireTimeSpan will update and its not reflected here
            var expiresUtc = context.Properties.ExpiresUtc;
    
            if ( expiresUtc == null)  return Task.FromResult(0);
    
            var sessionExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(sessionTimeInMinutes);
    
            var expiresDifferenceSeconds =
                sessionExpiresUtc.ToUnixTimeSeconds() - expiresUtc?.ToUnixTimeSeconds();
    
            var expires = sessionTimeInMinutes * 60 / expiresDifferenceSeconds < 2
                ? sessionExpiresUtc
                : expiresUtc;
    
            context.Identity.UpdateClaim(CustomClaimTypes.Expires, expires.ToString());
    
            return Task.FromResult(0);
        }
    }
    

    然后我有一个过滤器,它添加了应用程序使用的过期 cookie

    DateTimeOffset expires;
    
    if (identity.HasClaim(c => c.Type == CustomClaimTypes.Expires))
    {
         expires = DateTimeOffset.Parse(identity.FindFirstValue(CustomClaimTypes.Expires));
    }
    else
    {
        expires = DateTimeOffset.UtcNow.AddMinutes(sessionTimeInMinutes);
    }
    
    var cookieHeaderValues = new List<CookieHeaderValue>
    {
        new ApplicationCookeHeaderValue("session.expiresAt", expires.ToString("u"), expires),
    };
    
    

    【讨论】:

    • 我喜欢你的想法,但你能解释一下这行代码吗? var expires = sessionTimeInMinutes * 60 / expiresDifferenceSeconds
    • 很久了。我想我只是用 2 分钟的硬编码值进行测试
    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 2011-01-05
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2012-05-19
    相关资源
    最近更新 更多