예제 #1
0
        public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            ViewBag.MenuItem  = CurrentMenuItem;
            ViewBag.ReturnUrl = "/UserAdministration";

            var user = await userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                model.UserName = "******";
                ModelState.AddModelError("", "No user found.");
                return(View());
            }

            model.UserName = user.UserName;

            if (ModelState.IsValid)
            {
                user.SecurityStamp = Guid.NewGuid().ToString("D");

                try
                {
                    var code = await userManager.GeneratePasswordResetTokenAsync(user.Id);

                    IdentityResult result = await userManager.ResetPasswordAsync(user.Id, code, model.Password);

                    if (result.Succeeded)
                    {
                        model.ResetComplete = true;
                        return(View(model));
                    }
                    else
                    {
                        model.ResetComplete = false;
                        AddErrors(result);
                        return(View(model));
                    }
                }
                catch (Exception ex)
                {
                    model.ResetComplete = true;
                    return(View(model));
                }
            }

            return(View(model));
        }
        public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            ViewBag.MenuItem  = CurrentMenuItem;
            ViewBag.ReturnUrl = "/UserAdministration";

            var user = await userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                model.UserName = "******";
                ModelState.AddModelError("", "No user found.");
                return(View());
            }

            model.UserName = user.UserName;

            if (ModelState.IsValid)
            {
                user.SecurityStamp = Guid.NewGuid().ToString("D");

                try
                {
                    String hashedNewPassword = userManager.PasswordHasher.HashPassword(model.Password);
                    var    userTemp          = unitOfWork.Repository <User>().Queryable().SingleOrDefault(u => u.Id == user.Id);
                    userTemp.PasswordHash = hashedNewPassword;
                    unitOfWork.Repository <User>().Update(userTemp);

                    HttpCookie cookie = new HttpCookie("PopUpMessage");
                    cookie.Value = "Password reset successfully";
                    Response.Cookies.Add(cookie);

                    unitOfWork.Complete();

                    return(RedirectToAction("Index"));
                }
                catch (Exception ex)
                {
                    model.ResetComplete = true;
                    return(View(model));
                }
            }

            return(View(model));
        }