ASP.NET Identity Provider SignInManager Keeps Returning Failure

后端 未结 10 1672
天涯浪人
天涯浪人 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();
                user.PasswordHash = hasher.HashPassword(user, "our_password");
    
                _context.Users.Add(user);
            }
    

提交回复
热议问题