.Net Core标识种子数据:无法使用种子凭据登录



我安装了Initializer,一切似乎都运行正常,所有详细信息都保存到数据库中,但当我尝试通过Web应用程序登录时,每次都会失败。当我在登录控制器中运行调试器时,它在命中后返回{Failed}:

var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);

初始化程序:

public class DbInitialiser : IDbInitialiser
{

private readonly ApplicationDbContext _db;
private readonly UserManager<IdentityUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public DbInitialiser(UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager, ApplicationDbContext db)
{
_db = db;
_userManager = userManager;
_roleManager = roleManager;
}
public void Initialise()
{
try
{
if (_db.Database.GetPendingMigrations().Count() > 0) 
{
_db.Database.Migrate();
}
}
catch (Exception ex)
{

}
if (_db.Roles.Any(r => r.Name == "Admin")) return;
_roleManager.CreateAsync(new IdentityRole("Admin")).GetAwaiter().GetResult();//makes sure this executes before proceceding with anything else
_roleManager.CreateAsync(new IdentityRole("Manager")).GetAwaiter().GetResult();

_userManager.CreateAsync(new Employee       
{
UserName = "Admin",
Email = "admin@gmail.com",
EmailConfirmed = true,
TwoFactorEnabled = false,
PhoneNumberConfirmed = true
//can set other properties, this is for the initial setup
}, "Abc123!Abc123!").GetAwaiter().GetResult();
IdentityUser user = _db.Users.Where(u => u.Email == "admin@gmail.com").FirstOrDefault();
_userManager.AddToRoleAsync(user, "Admin").GetAwaiter().GetResult();

}

(我的员工类扩展了IdentityUser(

我已经检查了我在其他类似帖子中提到的所有密码要求,所以我知道这与此无关,当我检查SSMS时,该用户的所有数据都在aspnetusers中,所以我不确定为什么它不允许我登录到种子的管理员用户

当我在登录控制器中运行调试器时,它会返回{Failed}

SignInManager<TUser>.PasswordSignInAsync方法的源代码中,我们可以发现它会根据提供的userName来检查用户。

public virtual async Task<SignInResult> PasswordSignInAsync(string userName, string password,
bool isPersistent, bool lockoutOnFailure)
{
var user = await UserManager.FindByNameAsync(userName);
if (user == null)
{
return SignInResult.Failed;
}
return await PasswordSignInAsync(user, password, isPersistent, lockoutOnFailure);
}

在您的代码中,您将UserName设置为";CCD_ 4";这与CCD_ 5不同;admin@gmail.com";。如果用户使用电子邮件帐户登录,则代码片段var user = await UserManager.FindByNameAsync(userName);将返回null,从而导致问题。

要解决此问题,您可以尝试将UserName设置为与Email相同的值(admin@gmail.com)。或者修改登录代码逻辑,通过电子邮件找到用户,然后用密码登录该用户,如下所示。

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
returnUrl = returnUrl ?? Url.Content("~/");
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var user = await _userManager.FindByEmailAsync(Input.Email);
//var istrue = await _userManager.CheckPasswordAsync(user, Input.Password);
var result = await _signInManager.PasswordSignInAsync(user, Input.Password, Input.RememberMe, lockoutOnFailure: true);

//var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);

您需要在Caps中设置NormalizedEmail = "ADMIN@GMAIL.COM"。像这样:

_userManager.CreateAsync(new Employee       
{
UserName = "Admin",
Email = "admin@gmail.com",
NormalizedEmail = "ADMIN@GMAIL.COM",
EmailConfirmed = true,
TwoFactorEnabled = false,
PhoneNumberConfirmed = true
//can set other properties, this is for the initial setup
}, "Abc123!Abc123!").GetAwaiter().GetResult();

相关内容

  • 没有找到相关文章

最新更新