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值。
應該提供密碼,這給了我足夠的線索,以解決我的問題,以及...顯然是在數據庫中的密碼並未散列。 –