.Net 核心身份种子数据:无法使用种子凭证登录

问题描述 投票:0回答:2

我有我的 Initialiser 设置,一切似乎都运行正常,所有细节都保存到数据库中,但是当我尝试通过 webapp 登录时,它每次都失败。当我在登录控制器中运行调试器时,它会在点击后返回 {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 = "[email protected]",
                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 == "[email protected]").FirstOrDefault();
            _userManager.AddToRoleAsync(user, "Admin").GetAwaiter().GetResult();


        }

(我的员工类扩展了 IdentityUser)

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

sql-server database entity-framework asp.net-core asp.net-identity
2个回答
1
投票

当我在登录控制器中运行调试器时,它返回 {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
设置为“
Admin
”,这与
Email
与“
[email protected]
”不同。如果用户使用电子邮件帐户登录,代码片段
var user = await UserManager.FindByNameAsync(userName);
将返回 null,从而导致问题。

要修复它,您可以尝试将

UserName
设置为与
Email
([email protected]) 相同的值。或者修改登录代码逻辑,通过邮箱找到用户,然后用密码登录该用户,如下所示。

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

0
投票
You need to set **NormalizedEmail = "[email protected]"** in Caps. Like this

_userManager.CreateAsync(new Employee       
            {
                UserName = "Admin",
                Email = "[email protected]",
                NormalizedEmail = "[email protected]",
                EmailConfirmed = true,
                TwoFactorEnabled = false,
                PhoneNumberConfirmed = true
                //can set other properties, this is for the initial setup
            }, "Abc123!Abc123!").GetAwaiter().GetResult();
© www.soinside.com 2019 - 2024. All rights reserved.