在浏览器或选项卡上注销关闭

时间:2015-06-08 06:28:22

标签: asp.net-mvc asp.net-identity asp.net-identity-2

我需要注销用户,以防他关闭运行我网站的浏览器/标签页。我已将isPersistant bool设置为false,但它不会将用户注销。如果选项卡已关闭,我想强制用户再次登录。我不想使用jQuery。

以下是我当前代码中的一些相关摘要:

的AccountController

var result = await SignInManager.PasswordSignInAsync(model.Username,
    model.Password, false, shouldLockout: true);`

Startup.Auth

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        // Enables the application to validate the security stamp when the user logs in.
        // This is a security feature which is used when you change a password or add an external login to your account.  
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(5),
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))                      
     }
});            

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

1 个答案:

答案 0 :(得分:0)

作为@AndreiV状态,无法检测用户何时关闭浏览器。

我选择实现SignalR并监视断开事件的连接,此时,根据某些条件,我取消对用户会话的身份验证。如果他然后回到该网站,我知道他的会话不再有效,他被重定向到登录页面。