【问题标题】:How to log out user after closing browser ASP.NET MVC关闭浏览器 ASP.NET MVC 后如何注销用户
【发布时间】:2020-12-27 22:56:20
【问题描述】:

我使用PasswordSignInAsync函数登录

var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.Remember, shouldLockout: false);

并且字段model.Remember (isPersistent) 为假。

在我的 Startup 类中,我有一些类似的代码

app.UseCookieAuthentication(new CookieAuthenticationOptions{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });

我想要实现的不仅是30分钟后注销用户,还包括关闭浏览器后。

【问题讨论】:

  • 你不能。没有可靠的方法来检测浏览器关闭。
  • 你能分享你启动登录会话的代码吗?记住标志是否正确传递给它?如果是,cookie 应该是非持久的,这意味着它会在浏览器关闭时被删除。
  • 在调试模式下我可以清楚地看到记住标志是假的。也许我的浏览器有问题?我正在使用 Chrome,并且已经尝试取消选中 Continue where you left offContinue running background apps when Google Chrome is closed 等选项
  • 试试这个 - detect browser close。在浏览器关闭事件中,您可以注销。
  • 可能是肮脏的幼稚技术 - 您可以尝试在用户关闭浏览器时要求确认。获取它的事件并在该事件上编写一个调用 Controller 的 Logout 函数的 ajax 方法。

标签: c# asp.net asp.net-mvc authentication


【解决方案1】:

window.unload 来自客户端时,您是否尝试过清除 cookie? 也许这可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2020-10-23
    • 1970-01-01
    • 2014-07-01
    • 2017-01-19
    • 2012-06-16
    • 1970-01-01
    相关资源
    最近更新 更多