我已经在一个asp.netcore2mvc应用程序中实现了用户存储。请参阅下面的实现代码。我已经设置了启动选项,并且
lockoutOnFailure: true
上
PasswordSignInAsync()
方法。
由于某些原因,没有在用户存储上调用访问失败的方法。只调用“getLockoutenabledAsync()”。
当前的实现对于常规登录非常有效。我可以在没有任何问题的情况下登录。但是当测试失败的登录时,我不确定我缺少了什么来让它正确使用锁定和失败计数。
public class MyUser : IdentityUser<int>
{
//...
//AccessFailedCount, LockoutEnd, and LockoutEnabled are apart of IdentityUser
//...
}
public class AccountController : Controller
{
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
//...
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
//...
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//...
services.AddIdentity<MyUser, MyRole>(a =>
{
//...
a.Lockout.AllowedForNewUsers = true;
a.Lockout.DefaultLockoutTimeSpan = new System.TimeSpan(0, 5, 0);
a.Lockout.MaxFailedAccessAttempts = 5;
})
.AddDefaultTokenProviders()
.AddSignInManager<SignInManager<MyUser>>()
.AddUserStore<MyUserStore>()
.AddRoleStore<MyRoleStore>();
//...
}
}
public class MyUserStore : IUserStore<MyUser>, IUserRoleStore<MyUser>,
IUserPasswordStore<MyUser>, IUserEmailStore<MyUser>, IUserLockoutStore<MyUser>
{
//...
#region IUserLockoutStore interface
public async Task<DateTimeOffset?> GetLockoutEndDateAsync(MyUser user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot get lockout end date. User is null.");
}
return user.LockoutEnd;
}
public async Task SetLockoutEndDateAsync(MyUser user, DateTimeOffset? lockoutEnd, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot set lockout end date. User is null.");
}
user.LockoutEnd = lockoutEnd;
}
public async Task<int> IncrementAccessFailedCountAsync(MyUser user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot update AccessFailedCount. User is null.");
}
user.AccessFailedCount += 1;
return user.AccessFailedCount;
}
public async Task ResetAccessFailedCountAsync(MyUser user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot update AccessFailedCount. User is null.");
}
user.AccessFailedCount = 0;
}
public async Task<int> GetAccessFailedCountAsync(MyUser user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot get AccessFailedCount. User is null.");
}
return user.AccessFailedCount;
}
public async Task<bool> GetLockoutEnabledAsync(MyUser user, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot get LockoutEnabled. User is null.");
}
return user.LockoutEnabled;
}
public async Task SetLockoutEnabledAsync(MyUser user, bool enabled, CancellationToken cancellationToken)
{
if (user == null)
{
throw new ArgumentException("Cannot set LockoutEnabled. User is null.");
}
user.LockoutEnabled = enabled;
}
#endregion
}