如何在ASP.NET身份中更新UserManager?

时间:2016-07-07 12:36:17

标签: asp.net-mvc asp.net-identity-2

美好的一天,

我在asp.net Identity 2.0 MVC5中创建并为用户分配密码(并且几乎不是新的)并在登录时;我指示用户创建(更新)他/她的帐户。

一切顺利,但当用户提交更新时,它并不反映在数据库内部。它似乎错过了我的代码中的正确逻辑。这是我的ViewModel和Controller。

拜托,我需要你的帮助。

    public class EditUserViewModel
        {
            [Key]
            public string Id { get; set; }

            [Required]
            [Display(Name = "Surname")]
            public string FirstName { get; set; }

            [Required]
            [Display(Name = "Last Name")]
            public string LastName { get; set; }

            [Required]
            [Display(Name = "Username")]
            public string Username { get; set; }

            [Required]
            [EmailAddress]
            [Display(Name = "Email")]
            public string Email { get; set; }

            [Required]
            [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

            [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
            public string ConfirmPassword { get; set; }

            [Display(Name = "Gender")]
            public string Gender { get; set; }

            [Required]
            [Display(Name = "Mobile Phone")]
            public string PhoneNumber { get; set; }

            //[Required]
            [Display(Name = "Birthday - yyyy/mm/dd")]
            public DateTime DateOfBirth { get; set; }

            //public DateTime PostedDate { get; set; }
        }


    Controller:
    [HttpGet, ActionName("profile")]
            public ActionResult Edit()
            {

                return  View();
            }

            [HttpPost, ActionName("profile")]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Edit(EditUserViewModel editUser)
            {
                if (ModelState.IsValid && TempData["Username"] != null)
                {
                    var user = await UserManager.FindByNameAsync(editUser.Username);

                    if (user == null)
                    {
                        return HttpNotFound(); //change error later
                    }
                    user.FirstName = editUser.FirstName;
                    user.LastName = editUser.LastName;
                    user.UserName = editUser.Email;
                    user.Email = editUser.Email;
                    user.PasswordHash = editUser.Password;


                    var userUpdated = await UserManager.UpdateAsync(user);

                    if (!userUpdated.Succeeded)
                    {
                        ModelState.AddModelError("", userUpdated.Errors.First());
                        return View();
                    }
                    return RedirectToAction("index", "pages");
                }
                ModelState.AddModelError("", "Something failed!!!");
                return View();
    }

1 个答案:

答案 0 :(得分:0)

您不能使用user.PasswordHash,因为它是由UserManager专门设置的。您需要生成密码重置令牌以重置用户密码,如下所示:

[HttpPost, ActionName("profile")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Edit(EditUserViewModel editUser)
{
    if (ModelState.IsValid && TempData["Username"] != null)
    {
        var user = await UserManager.FindByNameAsync(TempData["Username"] as string);
        if (user == null)
        {
            return HttpNotFound(); //change error later
        }

        user.FirstName = editUser.FirstName;
        user.LastName = editUser.LastName;
        user.UserName = editUser.Email;
        user.Email = editUser.Email;

        var userUpdated = await UserManager.UpdateAsync(user);

        if (!string.IsNullOrWhiteSpace(editUser.Password))
        {
            UserManager.UserTokenProvider = new EmailTokenProvider<IdentityUser>();
            var resetToken = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            var result = await UserManager.ResetPasswordAsync(user.Id, resetToken, editUser.Password);
        }

        if (!userUpdated.Succeeded)
        {
            ModelState.AddModelError("", userUpdated.Errors.First());
            return View();
        }
        return RedirectToAction("index", "pages");
    }

    ModelState.AddModelError("", "Something failed!!!");
    return View();
}