public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
 {
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId<int>(), model.OldPassword, model.NewPassword);
     if (result.Succeeded)
     {
         var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
         if (user != null)
         {
             await SignInAsync(user, isPersistent: false);
         }
         return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
     }
     AddErrors(result);
     return View(model);
 }
Exemplo n.º 2
0
        public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
            if (result.Succeeded)
            {
                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                if (user != null)
                {
                    await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                }

                var currentUser = User.Identity.Name;
                var updClntPswrd = db.ClientUsers.FirstOrDefault(p => p.Username == currentUser);
                if (TryUpdateModel(updClntPswrd, "",
               new string[] { "ContactName", "Email" }))
                {
                    try
                    {
                        updClntPswrd.Password = model.NewPassword;


                        db.SaveChanges();

                        ViewBag.Message = "Record successfully Updated";

                        //return RedirectToAction("Index");
                    }
                    catch (DataException /* dex */)
                    {
                        //Log the error (uncomment dex variable name and add a line here to write a log.
                        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                    }
                }

                return RedirectToAction("Index","ClientUser", new { Message = ManageMessageId.ChangePasswordSuccess });
            }
            AddErrors(result);
            return View(model);
        }