0

I'm making a web application with ASP .NET Core 3.1. I'm using Identity to build user login function.

I'm getting a problem in which registered user can't login, due to password checking always fail. I went ahead and create an user programmatically, then check password right after the creation, and the check still fail.

Did I forget to set something up?

More details:

  1. I want user with roles so I set up custom Identity service like this. I think I forgot to set up some extra thing here.
// In Startup class
public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddIdentity<IdentityUser, IdentityRole>((_options) =>
    {
        // Custom IdentityOptions configuration;
    })
        .AddDefaultUI()
        .AddDefaultTokenProviders()
        .AddEntityFrameworkStores<ApplicationDbContext>();
   
    // ...
}
  1. I tried create user programmatically and check the password right after that. The check still fail.
// In Startup.Configure() method

string username = "admin";
string password = "123456";

IdentityUser user = new IdentityUser(username);
await userManager.CreateAsync(user, password);

bool isCorrect = await userManager.CheckPasswordAsync(user, password);
// isCorrect is always false;

(I do make sure that the user is created successfully, and password is added successfully. I did view the database data and see the user there.)

I did check similar questions on StackOverflow, but their problem seems to be different from mine so it didn't help.

4
  • Does the answer that I post solve your question? Commented Sep 2, 2022 at 1:25
  • I set EmailConfirmed to True in data view, but still couldn't login. Do I need to have email set as well? Commented Sep 2, 2022 at 1:49
  • I think you need. Have a look at this, to know more about identity. Commented Sep 2, 2022 at 1:54
  • Check how to perform authentication in Identity, see this link. Commented Sep 2, 2022 at 10:07

1 Answer 1

1

UserManager.CreateAsync method returns an IdentityResult that will tell you if user creation succeeded or not.

string username = "admin";
string password = "123456";

IdentityUser user = new IdentityUser(username);
IdentityResult result = await userManager.CreateAsync(user, password);

if (result.Succeded)
{
    
    var actualUser = await userManager.FindByNameAsync(username);

    if (actualUser != null)
    {
        bool isCorrect = await userManager.CheckPasswordAsync(actualUser, password);
    }
}
else
{
    // check what went wrong
    var errors = result.Errors;
}

User creation is probably failing because password is weak.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.identity.usermanager-1.createasync?view=aspnetcore-6.0

Sign up to request clarification or add additional context in comments.

3 Comments

As stated in question detail, I made sure user creation is successful. I see the user detail in database, with password hash.
Try to retrieve user using UserManager.FindByNameAsync and pass that user to CheckPasswordAsync.
Update: the issue solved itself after I restart Visual Studio. I took a look at database data after that and see a different password hash (didn't touch code, just restart VS). I think the password got "stuck" and didn't update with the new password I set and check against.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.