Home > Enterprise >  PasswordSignInAsync returns always failed
PasswordSignInAsync returns always failed

Time:11-17

In ApplicationDBContext.cs I have added the following code to seed the test user login information.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");var hasher = new PasswordHasher<ApplicationUser>();
    modelBuilder.Entity<ApplicationUser>().HasData(new ApplicationUser
    {
        Id = "1",
        FirstName = "John",
        LastName = "Smith",
        DateCreated = DateTime.Now,
        UserName = "[email protected]",
        NormalizedUserName = "John Smith",
        Email = "[email protected]",
        NormalizedEmail = "[email protected]",
        EmailConfirmed = true,
        LockoutEnabled = false,
        PasswordHash = hasher.HashPassword(null, "A1234g89"),
    });
}

In the UserController.cs in the login function I have the following line of code that logs the user based on username and password combination.

var result = await _signInManager.PasswordSignInAsync("[email protected]", "A1234g89", false, false);

However the value of result is always failed. Any idea what I'm doing wrong in here?

CodePudding user response:

An alternative solution would be to use the following lines of code and make changes to your code accordingly.

//Get the matched email user details
var SingleUser = _userManager.Users.SingleOrDefault(x => x.Email == model.Email);

var verfiyPassword = _userManager.PasswordHasher.VerifyHashedPassword(SingleUser, SingleUser.PasswordHash, model.Password);

//If the verification failed.
if (verfiyPassword != PasswordVerificationResult.Success)
{

}

//If the verification success.
if (verfiyPassword == PasswordVerificationResult.Success)
{

}
  • Related