ASP.NET标识 - 强制使用安全戳重新登录

Mat*_*rts 10 asp.net authentication asp.net-mvc asp.net-mvc-5 asp.net-identity

那么什么是ASP.NET Identity的IUserSecurityStampStore <TUser>接口?我们了解到ASP.NET Identity具有安全标记功能,用于使用户登录cookie无效,并强制他们重新登录.

在我的MVC应用程序中,管理员可以归档用户.当拱形时,它们应该立即被注销并被迫再次登录(这将因为它们被存档而拒绝它们).

我怎样才能做到这一点?我知道安全标记是关键.默认设置如下所示:

    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(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });
Run Code Online (Sandbox Code Playgroud)

通过实验,如果我将validateInterval设置为1分钟,然后在数据库中手动破解用户安全标记,那么他们将被迫重新登录,但仅在该时间段过去之后.

有没有办法让这个瞬间,或者只是将间隔设置为一个低时间段并等待(或实现我自己的OnValidateIdentity检查每个请求)

谢谢

Hao*_*ung 4

您正确地陈述了您的选项,要么是低间隔/等待,要么是挂钩您自己的自定义OnValidateIdentity.

这是一个类似的问题:立即传播角色变化