IdentityServer4如何设置服务器cookie过期时间

6
到目前为止,我已经看到了如何为客户端 Web 应用程序的 cookie 设置过期时间(谢谢 v0id):IdentityServer4 cookie expiration
IdentityServer4 实际上使用了两个 cookie——客户端 cookie 和服务器 cookie("idsrv")。
如果按照此处提供的方法设置客户端 cookie 的过期时间:IdentityServer4 cookie expiration,那么当我关闭浏览器并返回需要授权的客户端 Web 应用程序页面时,我会因为浏览器会话不再具有服务器 cookie 而被拒绝访问。
因此,我需要一种方法将 "idsrv" cookie 的过期时间设置为与客户端相同。
目前,我认为在 IdentityServer4 主机 Startup.cs / ConfigureServices() 方法中使用以下代码块是设置服务器 cookie(它被忽略或丢弃了)的最佳方式:
services.AddIdentityServer(options =>
            {
                options.Authentication.CookieLifetime = new TimeSpan(365, 0, 0, 0);
                options.Authentication.CookieSlidingExpiration = true;
            })

这将把cookie的过期时间设置为一年后。但是,在Chrome开发者工具中的应用程序标签下的cookies中,我看到它仍然有一个1969年过期的默认日期。

我下载了IdentityServer4项目源代码,删除了nuget包,并将源代码添加到我的解决方案中,以便我可以调试它。

我看到它在ConfigureInternalCookieOptions.cs / Configure()方法中获得了我给它的过期时间。它也匹配了DefaultCookieAuthenticationScheme内部/应用属性。我没有找到任何特定于IdentityServer的内容会忽略我设置的过期日期,但它仍然有1969年的过期时间。

编辑:我尝试在IdentityServer主机的AccountController中设置cookie持久性,如下所示(有趣的是,Microsoft在此处有一篇关于使用authenticationproperties而不使用AspNet Identity的好文章:https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?tabs=aspnetcore2x - 它正在发送一个cookie中的信息,“scheme”只是cookie名称): 在ExternalLoginCallback()中:

if (id_token != null)
        {
            props = new AuthenticationProperties();
            props.ExpiresUtc = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration);
            props.IsPersistent = true;
            props.StoreTokens(new[] { new AuthenticationToken { Name = "id_token", Value = id_token } });
        }

所有服务器端的Cookie均未设置过期时间(“AccountOptions RememberMeLoginDuration”也设置为365天)。“idsrv”和“idsrv.session”两个Cookie仍保持着1969年的到期时间。


2
你真的能找到解决方案吗?我也遇到了同样的问题.... - Leon
解决了这个问题(至少在我的情况下)- 你是否使用了他们的示例“快速入门”UI登录页面? - Leon
@JakeJ 你是怎么获取id_token的? - prisar
2个回答

5

您可以在注册Identity Server时,在Startup.cs中配置认证cookie的生存时间,如下所示:

services.AddIdentityServer(options =>
{
    options.Authentication.CookieLifetime = TimeSpan.FromHours(10);
})

注意:在登录用户时还需要指示 cookie 应该是持久的。如果您使用的是 Quickstart UI,则必须在登录屏幕上选中“记住我”复选框以获取持久 cookie。或者,您可以修改代码以始终发出持久 cookie - 类似于以下内容:
HttpContext.SignInAsync(subject, name, new AuthenticationProperties{ IsPersistent = true});

我正在调试IdentityServer4源代码,因为它正在接收选项。我看到它知道我给了它过期时间。但是,生成的cookie已经过期/基于浏览器会话(对于“idsrv” cookie)。我觉得最终我会提交一个拉取请求并发现他们是有意这样做的。你这边可以正常工作吗? - JakeJ
客户端cookie“Cookies”已被设置为持久性。您如何设置服务器cookie“idsrv”或“idsrv.session”为持久性?我看到了设置“idsrv”滑动和过期的选项,但它被忽略了。 - JakeJ
你正在使用快速入门UI吗? - Peter
让我们在聊天中继续这个讨论 - JakeJ
@JakeJ 我也遇到了和你一模一样的问题,自从我从 .net core 2.0 升级到 2.1 版本后就出现了这个问题。不知道你是否已经解决了这个问题? - user1015196
显示剩余8条评论

0
我使用以下代码设置了IdentityServer cookie配置。然后,当我通过IdentityServ的(记住我)选项存储cookie时。
 // Set identity cookie options
 services.ConfigureApplicationCookie(options =>
 {
     options.ExpireTimeSpan = TimeSpan.FromDays(30);
     options.SlidingExpiration = true;
     options.Cookie.SecurePolicy = Microsoft.AspNetCore.Http.CookieSecurePolicy.SameAsRequest;
 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接