Base-64 char数组或字符串的长度无效.在UserManager.CheckPasswordAsync(用户,密码)中 [英] Invalid length for a Base-64 char array or string. in UserManager.CheckPasswordAsync(user, password)

查看:323
本文介绍了Base-64 char数组或字符串的长度无效.在UserManager.CheckPasswordAsync(用户,密码)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习MVC,并使用自定义现有数据库创建登录和注册,我遵循了本教程 我的自定义登录管理器和用户存储区的代码是

I'm learning MVC and for creating Login and Register with Custom Existing Database I followed 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 code for my custom signinmanager and User store is

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

我不知道原因.有人可以帮忙吗?

I can't figure out the reason. Can anybody help?

推荐答案

感谢@trailmax的帮助,我能够解决此问题.我直接保存密码,并且在使用默认实现而不覆盖系统时,系统正在检查哈希密码.因此,我需要保存哈希密码或重写CheckPassword方法以进行手动检查.双向均可.

Thanks to help from @trailmax I was able to solve this problem. I was saving password directly and as I was using default implementation without overriding system was checking for hashed password. So, I needed to either save hashed password or override CheckPassword method for checking manually. It works both ways.

这篇关于Base-64 char数组或字符串的长度无效.在UserManager.CheckPasswordAsync(用户,密码)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆