SetLockoutEnabledAsync SetLockoutEndDateAsync

控制台显示警告:数据库未被更改

Lockout is not enabled for this user.

AspNetUsers表中字段LockoutEnabled表示“是否允许锁定该用户”,也就是说为用户启用锁定功能,而不是“该用户是否被锁定”的判断标准,默认true,只有该字段值为TRUE时,SetLockoutEndDateAsync才能调用不报上面的错误提示

可以从UserManager.IsLockedOutAsync方法看到.net6对账号锁定的定义

public virtual async Task<bool> IsLockedOutAsync(TUser user)
        {
            ThrowIfDisposed();
            var store = GetUserLockoutStore();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (!await store.GetLockoutEnabledAsync(user, CancellationToken))
            {
                return false;
            }
            var lockoutTime = await store.GetLockoutEndDateAsync(user, CancellationToken);
            return lockoutTime >= DateTimeOffset.UtcNow;
        }

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

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

            var result = await userManager.SetLockoutEnabledAsync(user, true);//确保为真var result1 = await userManager.SetLockoutEndDateAsync(user, isLock? DateTimeOffset.MaxValue:null);