2014-02-24 31 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

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

相關問題