ASP.NET Identity Provider SignInManager Keeps Retu

2019-03-11 23:41发布

I have an issue with the standard ASP Identity provider for MVC5. As soon as I log in the method:

await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

keeps returning Failure. So I started debugging, By using:

UserManager.FindByEmail(model.Email);

This returns a valid UserID for my user trying to log in. Then I used:

SignInManager.UserManager.CheckPassword(UserIDObtainedFromFind, model.Password);

And this returns true, which means that the password I provide is valid....

Sign In failure

Any ideas on how I can trace, of debug the SignInManager.PasswordSignInAsync method to see where it fails?

9条回答
家丑人穷心不美
2楼-- · 2019-03-12 00:39

In your startup check:

options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedPhoneNumber = false;

If these are set to true, you need to confirm the email or phone number before you can login.

查看更多
SAY GOODBYE
3楼-- · 2019-03-12 00:40

Bit old now, but here's my tuppence on this issue.

I was doing some static data creation in a utility to ensure some standard things were present in the Identity database (roles and an administrator account).

I was creating the entities and talking directly to the context to create any missing roles or that user. The issue I had was that I wasn't setting the NormalizedUserName and NormalizedEmail fields. I was simply setting Email and UserName.

The final code I use (with EF Core 2.x) is something like:

        if (!_context.Users.Any(_ => _.Id.Equals(Users.AdministratorId)))
        {
            var user = new ApplicationUser
            {
                Id = Users.AdministratorId,
                UserName = Users.AdministratorEmail,
                Email = Users.AdministratorEmail,
                EmailConfirmed = true,
                NormalizedEmail = Users.AdministratorEmail.ToUpper(),
                NormalizedUserName = Users.AdministratorEmail.ToUpper(),
                SecurityStamp = Guid.NewGuid().ToString()
            };

            var hasher = new PasswordHasher<ApplicationUser>();
            user.PasswordHash = hasher.HashPassword(user, "our_password");

            _context.Users.Add(user);
        }
查看更多
姐就是有狂的资本
4楼-- · 2019-03-12 00:41

SignInManager.PasswordSignIn works off of user name, you should double check that the user name is the same as the email you are passing in.

查看更多
登录 后发表回答