ASP.net Identity 2.0退出另一个用户

hyp*_*erN 23 c# asp.net-mvc asp.net-identity

我正在使用asp.net MVC和ASP.net Identity 2.0.

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

我知道我可以通过电话退出当前用户

AuthenticationManager.SignOut();
Run Code Online (Sandbox Code Playgroud)

但是可以注销另一个用户吗?或者可能缩短他的会话时间 还是什么?

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

Hao*_*ung 17

如果您使用securitystampvalidator功能,当禁止用户时,只需调用:UpdateSecurityStamp(userId)以使下次检查时任何现有登录cookie无效.

有关更多信息SecurityStamp?

  • 它不会立即记录用户,安全标记上有validateInterval,您需要配置验证cookie的频率. (8认同)

tra*_*max 15

您需要在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
}
Run Code Online (Sandbox Code Playgroud)

当用户被禁止时,郝公说,然后更新安全标记.

我最近在博客上写过这篇文章