I use a popular library Microsoft.AspNetCore.Identity
, which I configured:
var builder = services.AddIdentity<User, Role>(o =>
{
o.Lockout.AllowedForNewUsers = true;
o.Lockout.MaxFailedAccessAttempts = 3;
});
builder = new IdentityBuilder(builder.UserType, typeof(Role), builder.Services);
builder.AddEntityFrameworkStores<RepositoryContext>()
.AddDefaultTokenProviders();
For now, MaxFailedAccessAttempts
is set to 3 tries but may change for the entire application in the future. How can I get this value in Actions? I would like to use the value in action to calculate how many tries are left before the account is locked.
int attemptsLeft = user.AccessFailedCount - UNKNOWN.MaxFailedAccessAttempts;
return Unauthorized($"Wrong password. {attemptsLeft} attempts left.");
The solution that I found is initializing MaxFailedAccessAttempts
and getting in action value from appsettings.json
, but I don't want to add another field in the configuration.
You can inject UserManager and get
MaxFailedAccessAttempts
fromOptions.Lockout.MaxFailedAccessAttempts