A funny thing happend to me. So funny, I'm still out of control.
Short story:
inspired by https://www.stevejgordon.co.uk/extending-the-asp-net-core-identity-signinmanager I created a custom SignInManager
with an override of the PasswordSignInAsync
method. Background of this requirement is to have a second log in page (with a PIN). However, when I'm trying to login from the new page (LoginPin.cshtml), the PasswordSignInAsync
is not hit.
And here's the funny thing: when I do the same from the Login.cshtml page it is hit!
Long story:
public class AuditableSignInManager<TUser> : SignInManager<TUser> where TUser : class
{
private readonly UserManager<TUser> _userManager;
private readonly ApplicationDbContext _db;
private readonly IHttpContextAccessor _contextAccessor;
public AuditableSignInManager(UserManager<TUser> userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<TUser> claimsFactory,
IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<TUser>> logger, ApplicationDbContext dbContext, IAuthenticationSchemeProvider schemes)
: base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
{
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
_contextAccessor = contextAccessor ?? throw new ArgumentNullException(nameof(contextAccessor));
_db = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
public override async Task<SignInResult> PasswordSignInAsync(TUser user, string password, bool isPersistent, bool lockoutOnFailure)
{
//skipped for readability reasons
var result = await base.PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);
}
}
Startup.cs & Dependency Injection:
services.AddScoped<SignInManager<ApplicationUser>, AuditableSignInManager<ApplicationUser>>();
Login.cshtml.cs
public class LoginModel : PageModel
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModel> _logger;
public LoginModel(SignInManager<ApplicationUser> signInManager, ILogger<LoginModel> logger)
{
_signInManager = signInManager;
_logger = logger;
}
// skipped
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
var result = await _signInManager.PasswordSignInAsync(Input.UserName, Input.Password, false, lockoutOnFailure: false);
}
}
LoginPin.cshtml.cs
public class LoginModelPin : PageModel
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModelPin> _logger;
public LoginModelPin(SignInManager<ApplicationUser> signInManager, ILogger<LoginModelPin> logger)
{
_signInManager = signInManager;
_logger = logger;
}
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
var result = await _signInManager.PasswordSignInAsync(usrPwd, usrPwd, false, lockoutOnFailure: false);
}
}
To be honest: it is not funny anymore. Could someone point me to the right direction?
Many thanks in advance.
N.