IdentityServer4 cookie过期时间

7

我已经阅读了大约一天的IdentityServer4问题线程,但仍然对会话/登录cookie的过期时间感到困惑。

如果我像这样从客户端设置cookie过期时间(我正在使用IdentityServer3客户端和IdentityServer4服务器,以便使ASP.NET 4.x Web应用程序进行身份验证):

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Cookies",
                ExpireTimeSpan = new TimeSpan(10, 0, 0),
                SlidingExpiration = true
            });

我可以打开Chrome开发者工具(F12),查看cookie并且发现它们在浏览器关闭时即过期(IdentityServer的所有cookie的过期日期都被设置为“1969-12-31T23:59:59.000Z”,换句话说,客户端的过期时间没有生效)。
无论我是否将客户端和服务器验证选项的UseTokenLifetime设置为true,情况都是如此:
客户端:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                 ...
                 UseTokenLifetime = true,
                 ...

服务器端:

services.AddAuthentication()
   .AddOpenIdConnect("MyLoginScheme", "A login scheme", options =>
          ...
          options.UseTokenLifetime = true;
          ...

我不确定如何使其采用我设置的客户端 Cookie 生命周期。


我又看到了这个页面(关于覆盖cookie处理程序):https://identityserver4.readthedocs.io/en/release/topics/signin.html,并且我将其中设置身份验证cookie过期时间的部分添加进去了。然而,在Chrome中,我在登录后仍然看到与上面相同的cookie过期时间。 - JakeJ
该线程基本上提供了与文档页面相同的建议。在将中间件设置为我的上面评论的文档链接之后,我看到IdSrv在那个cookie中放了更多的信息,但是根据Chrome开发人员工具的Storage/Cookies信息,过期时间并没有按照我的要求设置。 - JakeJ
好的,我把它弄坏了。如果我告诉IdentityServer使用我的cookie,然后将登录方案也更改为我的方案,它将不再从外部身份提供者获取子声明/在IdentityServer登录流程中触发异常。 - JakeJ
我发现了另一种设定 cookie 过期时间的方式...但还没有生效:在 IdentityServer 的 Startup 类中,ConfigureServices() 方法中添加以下代码:services.AddIdentityServer(options => { options.Authentication.CheckSessionName = "idsrv"; options.Authentication.CookieLifetime = new Timespan(15, 0, 0); })然而在 Chrome devtools 中仍然显示过期时间为 1969。如果你将鼠标悬停在 CookieLifetime 上,会看到它只能与 IdentityServer 提供的 cookie 处理程序一起使用。已尝试了 "idsrv" 和 "idsrv.external",但都无效。 - JakeJ
我已经接受了v0id的答案,因为它至少解决了部分问题。它设置了客户端Web应用程序用于跟踪用户的cookie的过期时间。实际上有两个cookie,一个是给客户端使用的,另一个是给身份验证服务器("idsrv")使用的。我将为服务器cookie提出单独的问题,并在此处链接回来。 - JakeJ
在这个GitHub主题和https://github.com/IdentityServer/IdentityServer4/issues/1549之间,发现将UseTokenLifetime = false设置为我的情况起作用了。谢谢! - Eugene Kulabuhov
1个回答

7

试试这个:

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            // …
            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = async n =>
                {
                    // Set persistent cookie, 
                    n.AuthenticationTicket.Properties.IsPersistent = true; 
                    // and the expiration
                    n.AuthenticationTicket.Properties.ExpiresUtc = DateTime.Today.AddDays(1); 
                },
            },
            // …
        }

关于IDS的Cookie过期时间,您可以在Identity Server的ConfigureServices中进行设置:
        services.Configure<IdentityOptions>(options =>
        {
            // …
            options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(1);
            // …
        });

厉害的v0id,客户端cookie可以工作!我不确定是否有一种方法来设置idsrv用户会话cookie(“idsrv”)的过期时间,你知道这方面的任何信息吗? - JakeJ
目前我对checksession端点有些困惑。我可能可以再试一次将两者链接/让IdSrv使用客户端cookie,但是我认为我会再次遇到第三方身份提供者/获取索赔的问题。现在设置了客户端cookie,下一步就是解决这个问题。 - JakeJ
谢谢你。你使用的IdentityServer版本是哪个?那是IdentityServer3选项吗?也许在我的情况下不适用。客户端可用,你知道吗,你可以使用IdentityServer3客户端与IdentityServer4一起使用。 - JakeJ
@JakeJ 我正在使用 Identity.Server4 1.5.2。该类 IdentityOptions 来自 Microsoft.AspNetCore.Identity, Version=1.1.2.0 程序集。 - v0id
2
显然,只有在第三方身份提供者登录时点击“记住我”,它才会被设置为持久性,而我没有意识到这一点,也没有意识到Quickstart UI AccountController Login()方法中的AuthenticationProperties与cookie有任何关系。不过仍需要测试,而且可能需要我花一天时间解决这个新的数据库错误,以便我可以回到它上面。 - JakeJ
显示剩余5条评论

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