代码之家  ›  专栏  ›  技术社区  ›  Sat Thiru

AspNetCore:OpenId Cookies始终显示expires=1969-12-31,即使设置了IsPersistent=true和ExpireTimeSpan

  •  3
  • Sat Thiru  · 技术社区  · 8 年前

    使用ASPNETCORE OpenId认证中间件和Cookie中间件。我总是看到来自OpenId身份验证的cookie被设置为在1969年12月31日过期(在Chrome调试器中)。我假设这意味着cookie是会话cookie;我想让它们成为持久的cookie,这样用户将被提示减少登录频率。因此,我添加了ExpireTimeSpan和IsPersistent=true,正如其他帖子所建议的那样,但我仍然看到我的cookie过期时间是1969-12-31。

    我做错了什么?

    enter image description here

            services.AddAuthentication(options =>
            {
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddAzureAd(options =>
            {
                Configuration.Bind("AzureAd", options);
            })
            .AddCookie(p =>
            {
                p.ExpireTimeSpan = TimeSpan.FromDays(30);
                p.SlidingExpiration = true;
            });
    
            services.Configure<AuthenticationProperties>(props =>
            {
                props.IsPersistent = true;
                props.ExpiresUtc = new DateTimeOffset(DateTime.Now, TimeSpan.FromDays(30));
            });
    
    1 回复  |  直到 8 年前
        1
  •  4
  •   Sat Thiru    8 年前

    在aspnetcore安全论坛上获得帮助,并达成以下解决方案:

            .AddCookie(p =>
            {
                p.SlidingExpiration = true;
                p.Events.OnSigningIn = (context) =>
                {
                    context.CookieOptions.Expires = DateTimeOffset.UtcNow.AddDays(30);
                    return Task.CompletedTask;
                };
            });
    

    我还实现了一个注销页面(调用 AuthenticationHttpContextExtensions.SignOutAsync(HttpContext) )为用户提供对cookie生存期的更多控制。