Search code examples
c#asp.netasp.net-identityasp.net-membershipmembership

ASP.Net Identity - cannot login with password


Having some issue with ASP.Net Identity. I am creating a user via:

 var manager = new UserManager<IdentityUser>(userStore);

                manager.UserValidator = new UserValidator<IdentityUser>(manager)
                {
                    AllowOnlyAlphanumericUserNames = false
                };

                manager.PasswordValidator = new PasswordValidator
                {
                    RequiredLength = 6,
                    RequireDigit = true,
                    RequireLowercase = true,
                    RequireUppercase = true,
                };

                 var user = new IdentityUser() { UserName = email.Text, Email = email.Text };                    
                 IdentityResult result = manager.Create(user, Password1.Text);

This then adds the user to the database. When I then try and login with this user it always returns "Password incorrect" even when the password was the same password used to create the account.

I am logging in using:

var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);


        userManager.UserValidator = new UserValidator<IdentityUser>(userManager)
        {
            AllowOnlyAlphanumericUserNames = false
        };

        var user = userManager.Find(email.Text, Password.Text);

        if (user != null)

Using SQL Profiler I can see that that it looks up the user correctly and when I run the SQL in SSMS I can see the row is returned fine.

I'm not sure why userManager.Find is returning null when the actual record count returned from the database is 1?


Solution

  • Use the SignInManager

    SignInManager<IdentityUser> _signInManager; // Inject 
    
    var result = await _signInManager.PasswordSignInAsync(email.Text, Password.Text, true, lockoutOnFailure: true);