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

时间:2018-05-09 17:16:16

标签: cookies asp.net-identity session-cookies identity

使用ASPNETCORE OpenId身份验证中间件和Cookie中间件。我总是看到来自OpenId身份验证的cookie设置为在1969-12-31(在Chrome调试器中)到期。我认为这意味着cookies是SESSION cookies;我想让它们成为持久性cookie,以便提示用户不那么频繁地登录。所以我在其他帖子中添加了ExpireTimeSpan和IsPersistent = true,但我仍然看到我的cookie Expires是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 个答案:

答案 0 :(得分:4)

获得aspnetcore安全论坛的帮助,并得出以下解决方案:

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

我还实现了一个Logout页面(调用AuthenticationHttpContextExtensions.SignOutAsync(HttpContext)),让用户可以更好地控制cookie的生命周期。