Ale*_*der 5 c# asp.net-mvc lockout asp.net-identity
我需要永久阻止用户.我不明白为什么这段代码不起作用.
此行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();
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*tan 17
这条线
UserManager.SetLockoutEnabled(user.Id, true);
Run Code Online (Sandbox Code Playgroud)
没有锁定或解锁帐户.此方法用于永久启用或禁用给定用户帐户的锁定过程.就目前而言,您正在进行的呼叫基本上是将此用户帐户设置为受帐户锁定规则的约束.使用第二个参数进行调用,如false:
UserManager.SetLockoutEnabled(user.Id, false);
Run Code Online (Sandbox Code Playgroud)
允许您设置一个免于锁定规则的用户帐户 - 这可能对管理员帐户有用.
这是以下代码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;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,对于被归类为锁定的用户,必须如上所述启用锁定,并且用户必须具有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);
}
Run Code Online (Sandbox Code Playgroud)
该功能SetLockoutEnabled不会锁定用户,它为用户启用锁定功能
你需要
UserManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromHours(1); // lockout for 1 hour
UserManager.MaxFailedAccessAttemptsBeforeLockout = 5; // max fail attemps
await UserManager.AccessFailedAsync(user.Id); // Register failed access
Run Code Online (Sandbox Code Playgroud)
它将记录故障,并在启用锁定并达到故障计数时锁定用户.
在Login操作中将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);
Run Code Online (Sandbox Code Playgroud)