I implement the user.UserInvalidLogOnAttempts in the login action, after 5 failed attempts, i disabled the user user.Enabled = false, and then on the forgotpassword / resetpassword page after the user received the url with the token to change is password i enabled the user user.Enabled = true and reset to 0 the user.UserInvalidLogOnAttempts.
Login action:
// Attempts to authenticate the user against the Kentico database
SignInStatus signInResult = SignInStatus.Failure;
try
{
signInResult = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.SignInIsPersistent, false);
}
catch (Exception ex)
{
// Logs an error into the Kentico event log if the authentication fails
EventLogProvider.LogException("AccountController", "Login", ex);
}
// Get the user info
var user = UserInfoProvider.GetUserInfo(model.UserName);
//If the authentication was not successful, displays the sign-in form with an "Authentication failed" message
if (signInResult != SignInStatus.Success)
{
// Increment the number of invalid logon attempts.
++user.UserInvalidLogOnAttempts;
user.Update();
if (user.UserInvalidLogOnAttempts >= 5 || user.Enabled == false)
{
user.Enabled = false;
user.Update();
string errorMessageLocked = "Your account is disabled, please reset your password by clicking forgot password";
ModelState.AddModelError(string.Empty, errorMessageLocked);
}
else
{
string errorMessageFailed = "Authentication Failed" + " - " + user.UserInvalidLogOnAttempts + "/5 attempts";
ModelState.AddModelError(string.Empty, errorMessageFailed);
}
return View();
}
// Reset the invalid logon attempt count since the login was successful
user.UserInvalidLogOnAttempts = 0;
user.Update();
return RedirectToAction("...");
and in the resetpassword action:
if (ResetUserPassword(model.UserID, model.Token, model.Password).Succeeded)
{
// Get the user info, reset the invalid logon attempt count and enable the account
var user = UserInfoProvider.GetUserInfo(model.UserID);
user.Enabled = true;
user.UserInvalidLogOnAttempts = 0;
user.Update();
return RedirectToAction("ResetPasswordSucceeded");
}