Base-64 字符数组或字符串的长度无效。在 UserManager.CheckPasswordAsync(用户,密码)

Invalid length for a Base-64 char array or string. in UserManager.CheckPasswordAsync(user, password)

我正在学习 MVC,并且为了创建登录名并注册到自定义现有数据库,我遵循了 this tutorial and created custom User Manager and Sign In Manager. It allowed me to register a user properly and users are saved in my db properly. But password is saved as string without encryption as user entered it and also while login CheckPasswordAsync returns System.FormatException: Invalid length for a Base-64 char array or string. For Implementing UserPasswordStore I followed this tutorial 我的自定义登录管理器和用户存储的代码是

public class CustomUserManager : UserManager<MyAppUser>
{
    public CustomUserManager(IUserStore<MyAppUser> store) : base(store) { }

    internal static CustomUserManager Create()
    {
        return new CustomUserManager(new CustomUserStore());
    }

    public override Task<bool> CheckPasswordAsync(MyAppUser user, string password)
    {
        return base.CheckPasswordAsync(user, password);
    }
}

public class CustomUserStore : IUserStore<MyAppUser>, IUserPasswordStore<MyAppUser>
{
    private LearningDBContext database;

    public Task CreateAsync(MyAppUser user)
    {
        try
        {
            var context = userStore.Context as LearningDBContext;
            context.MyAppUsers.Add(user);
            context.Configuration.ValidateOnSaveEnabled = false;
            return context.SaveChangesAsync();
        }
        catch { }
        return Task.FromResult<bool>(true);
    }

    #endregion


    #region Password Store Region
    public Task SetPasswordHashAsync(MyAppUser user, string passwordHash)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.HasPasswordAsync(identityUser);
        setMyAppUser(user, identityUser);
        return Task.FromResult(0);
    }

    private void setMyAppUser(MyAppUser user, IdentityUser identityUser)
    {
        user.Password = identityUser.PasswordHash;
        user.Id = identityUser.Id;
        user.UserName = identityUser.UserName;
    }

    public Task<string> GetPasswordHashAsync(MyAppUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.GetPasswordHashAsync(identityUser);
        setMyAppUser(user, identityUser);
        return task;
    }

    public Task<bool> HasPasswordAsync(MyAppUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.HasPasswordAsync(identityUser);
        setMyAppUser(user, identityUser);
        return task;
    }

    private IdentityUser ToIdentityUser(MyAppUser user)
    {
        return new IdentityUser()
        {
            Id = user.Id,
            PasswordHash = user.Password,
            UserName = user.UserName
        };
    }
    #endregion
}

我在控制器中调用

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

我想不通原因。有人可以帮忙吗?

感谢@trailmax 的帮助,我得以解决这个问题。我直接保存密码,因为我使用默认实现而没有覆盖系统,所以正在检查散列密码。因此,我需要保存散列密码或重写 CheckPassword 方法以进行手动检查。它是双向的。