【问题标题】:MVC 5 Identity 2.0 lockout doesn't workMVC 5 Identity 2.0 锁定不起作用
【发布时间】:2019-03-13 15:08:02
【问题描述】:

我需要永久阻止用户。我不明白为什么这段代码不起作用。

这行UserManager.IsLockedOut(user.Id); 总是返回false 而不是true

也许有必要把这行UserManager.UserLockoutEnabledByDefault = true;放在用户注册阶段?

using (var _db = new ApplicationDbContext())
{
    UserStore<DALApplicationUser> UserStore = new UserStore<DALApplicationUser>(_db);
    UserManager<DALApplicationUser> UserManager = new UserManager<DALApplicationUser>(UserStore);
    UserManager.UserLockoutEnabledByDefault = true;
    DALApplicationUser user = _userService.GetUserByProfileId(id);
    bool a = UserManager.IsLockedOut(user.Id);
    UserManager.SetLockoutEnabled(user.Id, true);

    a = UserManager.IsLockedOut(user.Id);
    _db.SaveChanges();
}

【问题讨论】:

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


    【解决方案1】:

    线

    UserManager.SetLockoutEnabled(user.Id, true);
    

    没有锁定或解锁帐户。此方法用于永久启用或禁用给定用户帐户的锁定进程。就目前而言,您拨打的电话基本上是将此用户帐户设置为受帐户锁定规则的约束。调用第二个参数为false 即:

    UserManager.SetLockoutEnabled(user.Id, false);
    

    将允许您设置不受锁定规则约束的用户帐户 - 这可能对管理员帐户有用。

    这是UserManager.IsLockedOutAsync的代码:

    /// <summary>
    ///     Returns true if the user is locked out
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public virtual async Task<bool> IsLockedOutAsync(TKey userId)
    {
        ThrowIfDisposed();
        var store = GetUserLockoutStore();
        var user = await FindByIdAsync(userId).WithCurrentCulture();
        if (user == null)
        {
            throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.UserIdNotFound,
                userId));
        }
        if (!await store.GetLockoutEnabledAsync(user).WithCurrentCulture())
        {
            return false;
        }
        var lockoutTime = await store.GetLockoutEndDateAsync(user).WithCurrentCulture();
        return lockoutTime >= DateTimeOffset.UtcNow;
    }
    

    如您所见,要将用户归类为锁定,必须如上所述启用锁定,并且用户的LockoutEndDateUtc 值必须大于或等于当前日期。

    因此,要“永久”锁定帐​​户,您可以执行以下操作:

    using (var _db = new ApplicationDbContext())
    {
        UserStore<DALApplicationUser> UserStore = new UserStore<DALApplicationUser>(_db);
        UserManager<DALApplicationUser> UserManager = new UserManager<DALApplicationUser>(UserStore);
        UserManager.UserLockoutEnabledByDefault = true;
        DALApplicationUser user = _userService.GetUserByProfileId(id);
    
        bool a = UserManager.IsLockedOut(user.Id);
    
        //user.LockoutEndDateUtc = DateTime.MaxValue; //.NET 4.5+
        user.LockoutEndDateUtc = new DateTime(9999, 12, 30);
        _db.SaveChanges();
    
        a = UserManager.IsLockedOut(user.Id);
    }
    

    【讨论】:

      【解决方案2】:

      在您的登录操作中将 shouldLockout 值设置为 true(默认为 false)

                  // To enable password failures to trigger account lockout, change to shouldLockout: true
                  var result = await SignInManager.PasswordSignInAsync(vm.Email, vm.Password, vm.RememberMe, shouldLockout: true);
      

      【讨论】:

      • 这是解决此问题的最佳解决方案,因为它使用了 ASP.NET 标识的内置功能。
      【解决方案3】:

      SetLockoutEnabled 函数不会锁定用户,它为用户启用锁定功能

      你需要

      UserManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromHours(1); // lockout for 1 hour
      UserManager.MaxFailedAccessAttemptsBeforeLockout = 5; // max fail attemps
      await UserManager.AccessFailedAsync(user.Id); // Register failed access
      

      它会记录一次失败,如果启用了 Lockout 并且达到了失败计数,它将锁定用户。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-22
        • 2015-02-09
        • 2016-10-24
        • 1970-01-01
        • 2022-07-08
        • 2015-07-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多