PasswordSignInAsync returns 总是失败
PasswordSignInAsync returns always failed
在ApplicationDBContext.cs中我添加了以下代码来为测试用户登录信息做种。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");var hasher = new PasswordHasher<ApplicationUser>();
modelBuilder.Entity<ApplicationUser>().HasData(new ApplicationUser
{
Id = "1",
FirstName = "John",
LastName = "Smith",
DateCreated = DateTime.Now,
UserName = "john.smith@example.com",
NormalizedUserName = "John Smith",
Email = "john.smith@example.com",
NormalizedEmail = "john.smith@example.com",
EmailConfirmed = true,
LockoutEnabled = false,
PasswordHash = hasher.HashPassword(null, "A1234%6789"),
});
}
在 UserController.cs 的登录函数中,我有以下代码行根据用户名和密码组合记录用户。
var result = await _signInManager.PasswordSignInAsync("john.smith@example.com", "A1234%6789", false, false);
然而result的值总是失败。知道我在这里做错了什么吗?
另一种解决方案是使用以下代码行并相应地更改您的代码。
//Get the matched email user details
var SingleUser = _userManager.Users.SingleOrDefault(x => x.Email == model.Email);
var verfiyPassword = _userManager.PasswordHasher.VerifyHashedPassword(SingleUser, SingleUser.PasswordHash, model.Password);
//If the verification failed.
if (verfiyPassword != PasswordVerificationResult.Success)
{
}
//If the verification success.
if (verfiyPassword == PasswordVerificationResult.Success)
{
}
在ApplicationDBContext.cs中我添加了以下代码来为测试用户登录信息做种。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");var hasher = new PasswordHasher<ApplicationUser>();
modelBuilder.Entity<ApplicationUser>().HasData(new ApplicationUser
{
Id = "1",
FirstName = "John",
LastName = "Smith",
DateCreated = DateTime.Now,
UserName = "john.smith@example.com",
NormalizedUserName = "John Smith",
Email = "john.smith@example.com",
NormalizedEmail = "john.smith@example.com",
EmailConfirmed = true,
LockoutEnabled = false,
PasswordHash = hasher.HashPassword(null, "A1234%6789"),
});
}
在 UserController.cs 的登录函数中,我有以下代码行根据用户名和密码组合记录用户。
var result = await _signInManager.PasswordSignInAsync("john.smith@example.com", "A1234%6789", false, false);
然而result的值总是失败。知道我在这里做错了什么吗?
另一种解决方案是使用以下代码行并相应地更改您的代码。
//Get the matched email user details
var SingleUser = _userManager.Users.SingleOrDefault(x => x.Email == model.Email);
var verfiyPassword = _userManager.PasswordHasher.VerifyHashedPassword(SingleUser, SingleUser.PasswordHash, model.Password);
//If the verification failed.
if (verfiyPassword != PasswordVerificationResult.Success)
{
}
//If the verification success.
if (verfiyPassword == PasswordVerificationResult.Success)
{
}