0
在我的MVC(5.1)应用程序中。我想添加密码更改。MVC 5:使用UserManger更改密码时,Base-64字符数组或字符串的无效长度
这是我做了什么,
public class AccountController : Controller
{
private readonly ILicenserepository _licenserepository;
private readonly IUserRepository _userRepository;
public AccountController(ILicenserepository licenserepository, IUserRepository userRepository)
{
_licenserepository = licenserepository;
_userRepository = userRepository;
//UserManager = userManager;
}
public UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new DatePickerDbContext()));
....
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Manage(ManageUserViewModel model)
{
bool hasPassword = HasPassword();
ViewBag.HasLocalPassword = hasPassword;
ViewBag.ReturnUrl = Url.Action("Manage");
if (hasPassword)
{
if (ModelState.IsValid)
{
string userId = User.Identity.GetUserId();
IdentityResult result = UserManager.ChangePassword(userId, model.OldPassword, model.NewPassword);
if (result.Succeeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
}
else
{
AddErrors(result);
}
}
}
else
{
// User does not have a password so remove any validation errors caused by a missing OldPassword field
ModelState state = ModelState["OldPassword"];
if (state != null)
{
state.Errors.Clear();
}
if (ModelState.IsValid)
{
IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
if (result.Succeeded)
{
return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
}
else
{
AddErrors(result);
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
当我运行应用程序在我行获得浏览器的
Invalid length for a Base-64 char array or string.
此错误
IdentityResult result = UserManager.ChangePassword(userId, model.OldPassword, model.NewPassword);
我不知道怎么了。 是否因为我没有在构造函数中初始化我的UserManger
?
当我检查userId, model.OldPassword and model.NewPassword
中的值时,所有的值都符合我的预期。 userId保存userId值,oldpassword保存旧的passowrd值,新的密码保存用户提供的newpassword值。
应该提供密码,这给了我足够的线索,以解决我的问题,以及...显然是在数据库中的密码并未散列。 –