ASP.NET Identity Provider SignInManager Keeps Returning Failure

后端 未结 10 1659
天涯浪人
天涯浪人 2021-01-31 15:52

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.Pass         


        
相关标签:
10条回答
  • 2021-01-31 16:28

    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 讨论(0)
  • 2021-01-31 16:30

    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.

    0 讨论(0)
  • 2021-01-31 16:34

    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

    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>();
    //......
    }
    

    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!

    0 讨论(0)
  • 2021-01-31 16:34

    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 讨论(0)
提交回复
热议问题