46

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?

2
  • I was using a wrong connectionString :)) in case somene is like me! Commented Jul 23, 2018 at 9:46
  • @Reza, atleast not here because he is able to get Data for FindByEmail and CheckPassword Commented Dec 12, 2018 at 6:39

10 Answers 10

97

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.

4
  • 1
    Well you where correct. I used my username field to store The user's screen name. Reverting it back and adding a screen name field resolved my issue. Thanks! Commented Oct 17, 2014 at 20:59
  • 2
    You should revert back NormalizedUserName too, if you doing it manualy Commented Apr 19, 2016 at 9:08
  • 2
    as @AliGonabadi mentioned, just to add manually changing the username field will still result in failure and the NormalizedUserName must be also changed for this fix to work. Commented Aug 30, 2018 at 10:22
  • @MatthewFlynn good point. You´re absolutly right! Even for .NET CORE 2.0 your observation make a lot of diference. You´re the man!
    – user8207463
    Commented Nov 19, 2018 at 19:16
33

If username != email:

ApplicationUser signedUser = UserManager.FindByEmail(model.Email);
var result = await SignInManager.PasswordSignInAsync(signedUser.UserName, model.Password, model.RememberMe, shouldLockout: false);
2
  • Please use proper formatting when posting code. You can use the {} button for that just above the input field. Makes for better readability. Commented Feb 26, 2016 at 15:03
  • changed my username to no longer be email and was stuck on login failure for awhile thanks. Would just point out to others to also change the "model.Email" in the var UserID inside the SignInStatus.Success case to be "signedUser.UserName" too Commented Mar 4, 2020 at 21:14
11

In my case the same probe validating the email and password, the email is the same as my user.

    var userDO = _userManager.FindByEmailAsync(Input.Email).Result;
    var validatr = _userManager.CheckPasswordAsync(userDO, Input.Password);

email = userName

true result

Then I checked the database and found that the verification field of the user that did not work is false

enter image description here

Then the verification parameter must be validated in the startup. that is inside the Startup.cs

public void ConfigureServices(ServiceCollection services)
{
//......
            services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();
//......
}

enter image description here

The above property setting Gets or sets a flag indicating whether a confirmed IUserConfirmation account is required to sign in

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

Bye!

4

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.

2

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'
2
  • +1 for mentioning the Profiler, hadn't used it for years, forgot about it but it was just what I needed. Thanks!
    – Resource
    Commented Aug 11, 2017 at 9:39
  • Thanks for this. I was able to see that it was querying the default tables instead of the custom ones.
    – adam0101
    Commented Aug 16, 2018 at 1:02
1
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.

0

It's Work for me

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

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);
        }
0

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.

0

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.

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.