ASP.net Identity 2.0注销其他用户

本文关键字:注销 其他 用户 net Identity ASP | 更新日期: 2023-09-27 18:22:10

我使用的是asp.net MVC和asp.net Identity 2.0。

在我的网站管理员可以选择禁止用户,我希望当用户被禁止时,他会自动从网站注销。

我知道我可以通过调用注销当前用户

AuthenticationManager.SignOut();

但是可以注销另一个用户吗?或者缩短他的疗程?还是什么?

我知道我可以在控制器上进行全局过滤,禁止被禁止的用户访问,但这个过滤器会针对每个用户运行,所以我对这个解决方案并不满意。

ASP.net Identity 2.0注销其他用户

如果您使用securityampvalidator功能,当用户被禁止时,只需调用:UpdateSecurityStamp(userId)即可在下次检查任何现有登录cookie时使其无效。

有关SecurityStamp的更多信息?

您需要在Auth.Config.cs:中配置cookie无效

public void ConfigureAuth(IAppBuilder app)
{
    // important to register UserManager creation delegate. Won't work without it
    app.CreatePerOwinContext(UserManager.Create);
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator
                .OnValidateIdentity<UserManager, ApplicationUser, int>(
                    validateInterval: TimeSpan.FromMinutes(10),
                    regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager))
        },
        // other configurations
    });
    // other stuff
}

然后像郝功说的那样在用户被禁止时更新安全戳。

我最近在博客上写过这个