locked
Failing to increase AccessFailedCount using PasswordSignInAsync RRS feed

  • Question

  • User-203481489 posted

    Tried updating it with usermanager and also PasswordSignin but it doesnot update the AccessFailedCount, what i am missing ? please let me know, following is the code for login.

    ```

    // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, change to shouldLockout: true
                var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
                switch (result)
                {
                    case SignInStatus.Success:
                        return RedirectToLocal(returnUrl);
                    case SignInStatus.Failure:
                        var user = UserManager.FindByEmail(model.Email);
                        var data =SignInManager.UserManager.GetLockoutEnabled(user.Id);
                        await UserManager.AccessFailedAsync(user.Id);
                        UserManager.AccessFailed(user.Id);
                        return View(model);
                    default:
                        ModelState.AddModelError("", "Invalid login attempt.");
                        return View(model);
                }

    ```

    Wednesday, October 28, 2020 7:05 AM

All replies

  • User-939850651 posted

    Hi nanda kishore guduri,

    Based on your description, I did some searches on the issue. If it is just based on the current code, I think there is no problem.

    So I guess:

    var user = UserManager.FindByEmail(model.Email);

    Is it possible that this user does not exist? Do you debug this part of the code?

    If I misunderstood something, could you provide more details?

    Best regards,

    Xudong Peng

    Thursday, October 29, 2020 7:35 AM
  • User-203481489 posted

    User is available and i was able to increase the AccessFailedCount only once after using the below code even though i have been trying to give wrong password multiple times it always stays with 1

    UserManager.MaxFailedAccessAttemptsBeforeLockout = 5; // max fail attemps  
                        UserManager.SetLockoutEnabled(user1.Id, true);
                        UserManager.UserLockoutEnabledByDefault = true;
                        await UserManager.AccessFailedAsync(user1.Id);

    Thursday, October 29, 2020 9:46 AM
  • User-939850651 posted

    Hi nanda kishor guduri,

    I created a simple example to test your problem. The cause of the problem is this:

    UserManager.SetLockoutEnabled(user1.Id, true);

    It locks the user, so it will not increase the number of failed accesses.

    Here is my test:

    In page, I add it for show AccessFailedCount

    Access Failed Count: @ViewData["count"]


    Controller :
    // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { return View(model); } // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, change to shouldLockout: true var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true); var user = UserManager.FindByEmail(model.Email); switch (result) { case SignInStatus.Success: return RedirectToLocal(returnUrl); case SignInStatus.LockedOut:
    //I add these code for testing, you could delete if (user != null) { UserManager.ResetAccessFailedCount(user.Id); UserManager.UserLockoutEnabledByDefault = false; UserManager.SetLockoutEnabled(user.Id, false); } return View("Lockout"); case SignInStatus.RequiresVerification: return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); case SignInStatus.Failure: //var user = UserManager.FindByEmail(model.Email); UserManager.MaxFailedAccessAttemptsBeforeLockout = 5; // max fail attemps //UserManager.SetLockoutEnabled(user.Id, true); UserManager.UserLockoutEnabledByDefault = true; //UserManager.AccessFailed(user.Id); //await UserManager.AccessFailedAsync(user.Id); ViewData["count"] = UserManager.GetAccessFailedCount(user.Id); return View(model); default: ModelState.AddModelError("", "Invalid login attempt."); return View(model); } }

    Result:

    By the way, you don’t need to call UserManager.AccessFailed(user.Id) or UserManager.AccessFailedAsync(user.Id) to increase the AccessFailedCount. You could debug them to see the differences.

    Hope this can help you.

    Best regards,

    Xudong Peng

    Friday, October 30, 2020 9:54 AM