IdentityServer4 cookie 过期

Jak*_*keJ 5 cookies identityserver4

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

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

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

我可以打开 Chrome 开发人员工具 (F12) 并查看 cookie,并看到它们在浏览器关闭后立即过期(IdentityServer 的所有 cookie 的过期日期都设置为过期“1969-12-31T23:59: 59.000Z”,换言之,客户端未过期)。

无论我是否将客户端和服务器身份验证选项 UseTokenLifetime 都设置为 true,情况都是如此:

客户端:

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                 ...
                 UseTokenLifetime = true,
                 ...
Run Code Online (Sandbox Code Playgroud)

服务器端:

services.AddAuthentication()
   .AddOpenIdConnect("MyLoginScheme", "A login scheme", options =>
          ...
          options.UseTokenLifetime = true;
          ...
Run Code Online (Sandbox Code Playgroud)

我不确定如何让它占用我设置的客户端 cookie 生命周期。

v0i*_*0id 6

尝试这个:

        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); 
                },
            },
            // …
        }
Run Code Online (Sandbox Code Playgroud)

至于IDS的cookie过期时间,可以ConfigureServices在Identity Server的中设置:

        services.Configure<IdentityOptions>(options =>
        {
            // …
            options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(1);
            // …
        });
Run Code Online (Sandbox Code Playgroud)

  • 显然,如果您在第三方身份提供者登录时单击“记住我”,它只会被设置为持久性,我没有意识到,或者快速入门 UI AccountController Login() 方法中的 AuthenticationProperties 与 cookie 有任何关系。不过仍然需要测试,我可能需要一天时间来解开这个新的数据库错误,这样我才能回到它。 (2认同)