ASP.NET Identity Provider SignInManager Keeps Returning Failure

女生的网名这么多〃 提交于 2019-12-02 22:04:20

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.

Владимир Ш.

If username != email:

ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);

For me, I found it helpful to use SQL Profiler to see the query that PasswordSignInAsync was calling. In my case - I noticed it was trying to find a user with a Discriminator set to "UserContext". This of course wasn't working for me because I upgraded from ASP.NET Membership Services and this Discriminator was set to User. Since the new code uses Entity Framework, it appears this value is derived from the class you use for your user. A quick update statement fixed the issue.

UPDATE AspNetUsers SET Discriminator = 'UserContext' WHERE Discriminator = 'User'
Z.W.Huang

It's Work for me

ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);
ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, 
model.Password, model.RememberMe, shouldLockout: false);

This worked for me becouse my username was not equal my email. Your email and username should be the same.

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);
        }
Fernando Feliciano Jr.

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.

In case none of the above is your cause for the problem (in my case the problem was a copy-paste bug in my own implementation of IUserStore) and to answer your question "Any ideas on how I can trace, or debug the SignInManager.PasswordSignInAsync method to see where it fails?", one way to debug it would be to copy the contents of the method SignInManager.PasswordSignInAsync into your own derived class (ApplicationSignInManager).

You may find the source code in here or here if you're using MVC5 and lower.

Maybe my experience can help somebody. In my case, the problem was that I changed my computer, and in my new Windows 10, IIS feature was installed but not ASP.NET feature.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!