Example #1
0
        public async Task<ActionResult> Edit(EditUserViewModel user)
        {
            if (user != null && ModelState.IsValid)
            {
                AppUser controlUser = await UserManager.FindByIdAsync(user.Id);
                IdentityResult validUserName = IdentityResult.Success;
                IdentityResult validNewPassword = IdentityResult.Success;
                IdentityResult validEmail = IdentityResult.Success;

                if (controlUser.UserName != user.UserName)
                {
                    validUserName = IdentityResult.Failed(new string[]{string.Format("Не е разрешено модифициране на потребителското име!!!")});
                    AddErrorsFromResult(validUserName);
                }
                else
                {
                    if (!string.IsNullOrEmpty(user.NewPassword))
                    {
                        validNewPassword = await UserManager.PasswordValidator.ValidateAsync(user.NewPassword);
                        if (!validNewPassword.Succeeded)
                        {
                            AddErrorsFromResult(validNewPassword);
                        }
                    }

                    validEmail = await UserManager.ValidateEmailAsync(user.Email);
                    if (!validEmail.Succeeded)
                    {
                        AddErrorsFromResult(validEmail);
                    }
                }
                if (validUserName.Succeeded && validEmail.Succeeded && validNewPassword.Succeeded)
                {
                    controlUser.Email = user.Email;
                    controlUser.PasswordHash =
                        (!string.IsNullOrEmpty(user.NewPassword) &&
                        (!string.IsNullOrWhiteSpace(user.NewPassword)))
                        ? UserManager.PasswordHasher.HashPassword(user.NewPassword)
                        : controlUser.PasswordHash;
                    IdentityResult result = await UserManager.UpdateAsync(controlUser);
                    if (result.Succeeded)
                    {
                        this.TempData["success"] = string.Format("Потребителя {0} беше променен успешно.", controlUser.UserName);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        AddErrorsFromResult(result);
                    }
                }
            }
            else
            {
                if (user == null)
                {
                    ModelState.AddModelError("", "Потребителя не е намерен");
                }
            }
            return View(user);
        }
Example #2
0
 /// <summary>
 /// Validates the curent password async.
 /// </summary>
 /// <param name="user">The user.</param>
 /// <param name="controlUser">The control user.</param>
 /// <returns></returns>
 private Task<IdentityResult> ValidateCurentPasswordAsync(EditUserViewModel user, AppUser controlUser)
 {
     // TODO: Implement this method
     throw new NotImplementedException();
 }