【问题标题】:Asp.net mvc identity SecurityStamp signout everywhere无处不在的 Asp.net mvc 标识 SecurityStamp 注销
【发布时间】:2016-07-09 04:29:48
【问题描述】:

我想要做的是将用户 ID 限制为一次只能登录一台设备。例如,用户 ID“abc”登录到他们的计算机。用户 ID“abc”现在尝试从他们的手机登录。我想要发生的事情是终止他们计算机上的会话。

我正在使用 Asp.net mvc 身份成员身份并为此使用 SecurityStamp。这是我在帐户/登录操作中的代码:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        var user = UserManager.FindByEmail(model.Email);
        var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
        await UserManager.UpdateSecurityStampAsync(user.Id);

根据UpdateSecurityStampAsync 方法文档说:为用户生成一个新的安全标记,用于SignOutEverywhere 功能。但它不起作用。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-identity


    【解决方案1】:

    如果您想在其他设备上启用 cookie 即时失效,则每个请求都必须访问数据库以验证 cookie。为此,您需要在 Auth.Config.cs 中配置 cookie 失效并将 validateInterval 设置为 0:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        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<UserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromSeconds(0),
                        regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }            
    );
    

    【讨论】:

    • 是的。它对每个请求都有额外的数据库命中
    • 在 PasswordSignInAsync 之前尝试 UpdateSecurityStampAsync
    • 您无法注销或在其他设备上用户未注销?
    • tmg 当我将 validateInterval 设置为 0 时,它会很快注销用户
    猜你喜欢
    • 1970-01-01
    • 2010-11-11
    • 2015-03-31
    • 1970-01-01
    • 2020-01-21
    • 2013-12-21
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多