Пример #1
0
 public String ValidateFormInput(ChangeLoginDetailsViewModel model)
 {
     if (!ModelState.IsValid || (String.IsNullOrEmpty(model.OldEmail) && String.IsNullOrEmpty(model.OldPassword)))
     {
         return("Either current password or email must be provided.");
     }
     if (!String.IsNullOrEmpty(model.OldPassword))
     {
         if (String.IsNullOrEmpty(model.NewPassword) || String.IsNullOrEmpty(model.ConfirmPassword))
         {
             return("Both new and confirmed passwords must be provided.");
         }
     }
     if (!String.IsNullOrEmpty(model.OldEmail))
     {
         if (String.IsNullOrEmpty(model.NewEmail) || String.IsNullOrEmpty(model.ConfirmEmail))
         {
             return("Both new and confirmed emails must be provided.");
         }
     }
     return("ok");
 }
Пример #2
0
        public async Task <ActionResult> ChangeLoginDetails(ChangeLoginDetailsViewModel model)
        {
            var formValidationResult = ValidateFormInput(model);

            if (formValidationResult == "ok")
            {
                if (!String.IsNullOrEmpty(model.OldPassword))
                {
                    var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

                    if (result.Succeeded)
                    {
                        if (!String.IsNullOrEmpty(model.OldEmail))
                        {
                            var user = _db.AspNetUsers.Find(User.Identity.GetUserId());
                            if (user.Email == model.OldEmail)
                            {
                                if (IsValid(model.NewEmail))
                                {
                                    user.Email    = model.NewEmail;
                                    user.UserName = model.NewEmail;
                                    _db.SaveChanges();
                                }
                                else
                                {
                                    ModelState.AddModelError(String.Empty, "The new email does not have the correct format.");
                                    return(View());
                                }
                            }
                            else
                            {
                                ModelState.AddModelError(String.Empty, "The email you provided is incorrect.");
                                return(View());
                            }
                        }
                        return(RedirectToAction("MyPortal", "Home", new { Message = ManageMessageId.ChangePasswordSuccess }));
                    }
                    else
                    {
                        AddErrors(result);
                        return(View(model));
                    }
                }
                else
                {
                    var user = _db.AspNetUsers.Find(User.Identity.GetUserId());
                    if (user.Email == model.OldEmail)
                    {
                        if (IsValid(model.NewEmail))
                        {
                            user.Email    = model.NewEmail;
                            user.UserName = model.NewEmail;
                            _db.SaveChanges();
                        }
                        else
                        {
                            ModelState.AddModelError(String.Empty, "The new email does not have the correct format.");
                            return(View());
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty, "The email you provided is incorrect.");
                        return(View());
                    }
                    return(RedirectToAction("MyPortal", "Home", new { Message = ManageMessageId.ChangePasswordSuccess }));
                }
            }
            else
            {
                ModelState.AddModelError(String.Empty, formValidationResult);
                return(View());
            }
        }