[英]Invalid length for a Base-64 char array or string. in UserManager.CheckPasswordAsync(user, password)
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. 我正在学习MVC,并使用自定义现有数据库创建登录和注册,我按照本教程进行操作,并创建了自定义用户管理器和登录管理器。 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. 但是, 密码会保存为字符串,而不会在用户输入密码时加密,并且在用户登录CheckPasswordAsync时返回System.FormatException:Base-64字符数组或字符串的长度无效。 For Implementing UserPasswordStore I followed this tutorial code for my custom signinmanager and User store is 对于实现UserPasswordStore,我按照以下教程代码进行了自定义signinmanager的操作,而User store是
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
}
and in controller I'm calling 在控制器中,我打电话
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);
I can't figure out the reason. 我不知道原因。 Can anybody help? 有人可以帮忙吗?
Thanks to help from @trailmax I was able to solve this problem. 感谢@trailmax的帮助,我得以解决此问题。 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. 因此,我需要保存哈希密码或重写CheckPassword方法以进行手动检查。 It works both ways. 它可以双向工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.