ASP.NET Core 2.2 关闭浏览器选项卡时注销用户 - IsPersistent 无效

Way*_*yne 5 c# asp.net-core

我知道这个问题似乎已经有人问过了,但这与 ASP.NET Core 而不是 ASP.NET 5 有关。

当用户关闭浏览器选项卡时,我试图让用户注销;我正在 MacOS 上使用 Chrome 和 Safari 对此进行测试。

目前,当我登录用户并关闭并重新打开浏览器选项卡时,用户保持登录状态。

当我登录用户在我设定的是AuthenticationProperties IsPersistentfalse。然而,当浏览器选项卡关闭时,用户在 Chrome 和 Safari 上都保持登录状态。(浏览器没有关闭,只有选项卡)。

        Task task = HttpContext.SignInAsync(principal, 
           new AuthenticationProperties
           {
              IsPersistent = false
           });             
        await task;
Run Code Online (Sandbox Code Playgroud)

根据文档:persistent-cookies

“您可能希望 cookie 在浏览器会话中持续存在”

在上述情况下,我将 IsPersistent 设置为 false,并且我假设 cookie 不应在会话中存活。

据我了解,浏览器不会关闭会话,服务器会关闭,并且设置为以下 10 秒。

然而,通过如下测试,我无法让用户注销。

  1. 登录用户
  2. 确认用户已登录
  3. 关闭浏览器选项卡,(不是关闭浏览器)
  4. 等待超过 10 秒
  5. 打开浏览器选项卡并确保用户未登录。这失败

这可能无关紧要...... Starup.cs 有这个:

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;
            // Make the session cookie essential
            options.Cookie.IsEssential = true;

        });

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
        {
            options.LoginPath = "/auth/login";
            options.AccessDeniedPath = "/auth/accessdenied";
        });
Run Code Online (Sandbox Code Playgroud)

Way*_*yne 3

会话 Cookie 和身份验证 Cookie 是两个独立的 Cookie,因此options.IdleTimeout = TimeSpan.FromSeconds(10);对身份验证 Cookie 没有影响。

通过ExpireTimeSpan如下设置身份验证 cookie 选项,将导致用户在浏览器关闭或不活动超过 10 秒时注销。此外,SlidingExpiration如果在过期时间之前处于活动状态,则通过在旧 cookie 过期之前发出新 cookie,将导致不注销用户的预期效果。

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
        {
            options.LoginPath = "/auth/login";
            options.AccessDeniedPath = "/auth/accessdenied";
            options.Cookie.IsEssential = true;
            options.SlidingExpiration = true; // here 1
            options.ExpireTimeSpan = TimeSpan.FromSeconds(10);// here 2
        });
Run Code Online (Sandbox Code Playgroud)

但是,当该IsPersistent属性设置为 true 时,不会影响 cookie 过期时间。例如,它不会使 cookie 不会过期。 是为了在IsPersistent关闭中生存下来browsernot the tab