【发布时间】: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 off和Continue running background apps when Google Chrome is closed等选项 -
试试这个 - detect browser close。在浏览器关闭事件中,您可以注销。
-
可能是肮脏的幼稚技术 - 您可以尝试在用户关闭浏览器时要求确认。获取它的事件并在该事件上编写一个调用 Controller 的 Logout 函数的 ajax 方法。
标签: c# asp.net asp.net-mvc authentication