MVC 5 Identity 2.0 锁定不起作用

MVC 5 Identity 2.0 lockout doesn't work

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

这一行 UserManager.IsLockedOut(user.Id); 总是 returns 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();
}

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

你需要

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

它会记录一次失败,如果启用锁定并达到失败次数,则会锁定用户。

UserManager.SetLockoutEnabled(user.Id, true);

没有锁定或解锁帐户。此方法用于永久启用或禁用给定用户帐户的锁定 process。就目前而言,您正在进行的调用基本上是将此用户帐户设置为受帐户锁定规则的约束。使用第二个参数作为 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 值必须大于或等于当前日期。

因此,要"permanently"锁定帐户,您可以执行以下操作:

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);
}

在登录操作中将 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);