Home > front end >  Why is the login not working in ASP.NET Core?
Why is the login not working in ASP.NET Core?

Time:11-10

I have a user in the database, and the hash password is stored there, why does the login not work when logging in?

if (ModelState.IsValid)
{
    var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

    if (result.Succeeded)
    {
        return RedirectToAction("Privacy", "Home");
    }
}

builder.Entity<User>()
            .HasData(new User
                         {
                             FirstName = "Andrij",
                             LastName = "Matviiv",
                             Email = "[email protected]",
                             PasswordHash = hasher.HashPassword(null, "Andrew13mtv@")
                         };

This action method doesn't work even though I type correctly, how to fix?

CodePudding user response:

Because the method PasswordSignInAsync takes the username as a first parameter and you are passing email. try giving the username.

public virtual System.Threading.Tasks.Task<Microsoft.AspNetCore.Identity.SignInResult> PasswordSignInAsync (string userName, string password, bool isPersistent, bool lockoutOnFailure)

for reference click

and if you want to log in with email try this click

Updated

One more thing try specifying the user in the first parameter

    User user = new User
    {
        FirstName = "Andrij",
        LastName = "Matviiv",
        Email = "[email protected]",
    };
    var hashedPassword = hasher.HashPassword(user, "Andrew13mtv@");
    user.PasswordHash = hashedPassword;
    builder.Entity<User>()
        .HasData(user);

if the problem still occurs after passing the user as a parameter then plz try the above method of login with username otherwise try this to change the default login method to email. click

  • Related