AspNetCore:OpenId Cookie 始终显示 expires=1969-12-31,即使设置了 IsPersistent=true 和 ExpireTimeSpan
AspNetCore: OpenId Cookies always show expires=1969-12-31 even with IsPersistent=true and ExpireTimeSpan set
使用ASPNETCORE OpenId认证中间件和Cookie中间件。我总是看到来自 OpenId 身份验证的 cookie 设置为在 1969-12-31 过期(在 Chrome 调试器中)。我认为这意味着 cookie 是 SESSION cookie;我想让它们成为持久性 cookie,这样用户将被提示登录的频率降低。所以我按照其他帖子中的建议添加了 ExpireTimeSpan 和 IsPersistent=true,但我仍然看到我的 cookie Expires 是 1969-12-31。
我做错了什么?
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));
});
在 aspnetcore 安全论坛上得到帮助,得出以下解决方案:
.AddCookie(p =>
{
p.SlidingExpiration = true;
p.Events.OnSigningIn = (context) =>
{
context.CookieOptions.Expires = DateTimeOffset.UtcNow.AddDays(30);
return Task.CompletedTask;
};
});
我还实现了一个注销页面(调用 AuthenticationHttpContextExtensions.SignOutAsync(HttpContext)
)让用户更好地控制 cookie 的生命周期。
使用ASPNETCORE OpenId认证中间件和Cookie中间件。我总是看到来自 OpenId 身份验证的 cookie 设置为在 1969-12-31 过期(在 Chrome 调试器中)。我认为这意味着 cookie 是 SESSION cookie;我想让它们成为持久性 cookie,这样用户将被提示登录的频率降低。所以我按照其他帖子中的建议添加了 ExpireTimeSpan 和 IsPersistent=true,但我仍然看到我的 cookie Expires 是 1969-12-31。
我做错了什么?
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));
});
在 aspnetcore 安全论坛上得到帮助,得出以下解决方案:
.AddCookie(p =>
{
p.SlidingExpiration = true;
p.Events.OnSigningIn = (context) =>
{
context.CookieOptions.Expires = DateTimeOffset.UtcNow.AddDays(30);
return Task.CompletedTask;
};
});
我还实现了一个注销页面(调用 AuthenticationHttpContextExtensions.SignOutAsync(HttpContext)
)让用户更好地控制 cookie 的生命周期。