2014-02-24 32 views
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值。

回答

3

这是我愚蠢的错误 我已经创建的用户喜欢在此之前

var user = new ApplicationUser 
      { 
       FirstName = model.FirstName, 
       LastName = model.Lastname, 
       Phone = model.Phone, 
       Email = model.EmailId, 
       Company = model.Company, 
       PasswordHash = model.ConfirmPassword 
       UserName = model.UserName, 
       DateTimeRegistered = DateTime.UtcNow, 
       License = model.SelectedLicense, 
       IsApproved = true, 
      }; 
      var result = UserManager.Create(user); 

这里,密码不会被散列,并保存为精确的字符串用户提供。

这是创建错误。

在创建用户喜欢

var user = new ApplicationUser 
      { 
       FirstName = model.FirstName, 
       LastName = model.Lastname, 
       Phone = model.Phone, 
       Email = model.EmailId, 
       Company = model.Company, 
       UserName = model.UserName, 
       DateTimeRegistered = DateTime.UtcNow, 
       License = model.SelectedLicense, 
       IsApproved = true, 
      }; 
      var result = UserManager.Create(user,model.ConfirmPassword); 
+0

应该提供密码,这给了我足够的线索,以解决我的问题,以及...显然是在数据库中的密码并未散列。 –

相关问题