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