PixelPaul
PixelPaul

Reputation: 2801

.net Core 3 identity 'AccessFailedCount' not incrementing

I'm having a problem with a asp.net Core 3 identity project. I have created custom UserStore and RoleStore to use Dapper instead of Entity Framework. Most appears to be working, except upon a failed login attempt, the AccessFailedCount in the database is not being incremented. I have stepped through the code, and located where the logic is failing. It is in the SignInManager method named CheckPasswordSignInAsync, in this block:

if (UserManager.SupportsUserLockout && lockoutOnFailure)
{
    // If lockout is requested, increment access failed count which might lock out the user
    await UserManager.AccessFailedAsync(user);
    if (await UserManager.IsLockedOutAsync(user))
    {
        return await LockedOut(user);
    }
}

The value for UserManager.SupportsUserLockout is false, so it never hits the AccessFailedAsync method. But I'm not sure where this value comes from. The code block from UserManager.SupportsUserLockout in UserManager is:

public virtual bool SupportsUserLockout
{
  get
  {
    this.ThrowIfDisposed();
    return this.Store is IUserLockoutStore<TUser>;
  }
}

But I can't figure out what here sets it to false? Where does this value come from?

Upvotes: 3

Views: 1494

Answers (1)

Mahesh More
Mahesh More

Reputation: 855

I think I got your problem, you haven't implemented IUserLockoutStore<TUser> interface. As you are not using the Entity framework so you need to implement all necessary stores your self. Below are the functions which you need to implement.

Task<int> GetAccessFailedCountAsync(TUser user, CancellationToken cancellationToken);
Task<bool> GetLockoutEnabledAsync(TUser user, CancellationToken cancellationToken);
Task<DateTimeOffset?> GetLockoutEndDateAsync(TUser user, CancellationToken cancellationToken);
Task<int> IncrementAccessFailedCountAsync(TUser user, CancellationToken cancellationToken);
Task ResetAccessFailedCountAsync(TUser user, CancellationToken cancellationToken);
Task SetLockoutEnabledAsync(TUser user, bool enabled, CancellationToken cancellationToken);
Task SetLockoutEndDateAsync(TUser user, DateTimeOffset? lockoutEnd, CancellationToken cancellationToken);

Once you implement IUserLockoutStore<TUser> interface you have to enable Lockout functionality for an individual user, in order to achieve that you just need to set User.LockoutEnabled = 1 (true) in AspNetUsers table.

I hope this will resolve your issue!

Upvotes: 2

Related Questions