示例#1
0
 public ActionResult Settings(LocalPasswordModel model)
 {
     ViewBag.NotRead = HomeController.GetNotReadMessagesCount();
     UsersContext db = new UsersContext();
     List<UserData> TempUserList = new List<UserData>();
     TempUserList = db.UsersData.ToList();
     for (int i = 0; i < TempUserList.Count; i++)
     {
         if (TempUserList[i].UserProfile.UserId == WebSecurity.CurrentUserId)
         {
             ViewBag.currentUser = TempUserList[i];
             break;
         }
     }
     if (String.IsNullOrEmpty(model.ConfirmPassword) || String.IsNullOrEmpty(model.OldPassword)
         || String.IsNullOrEmpty(model.NewPassword))
     {
         ModelState.AddModelError("PasswordMessage", "Ошибка! Заполните все поля!");
         return View(model);
     }
             if(model.ConfirmPassword != model.NewPassword)
             {
                 ModelState.AddModelError("PasswordMessage", "Ошибка! Пароли не совпадают");
                 return View(model);
             }
       bool changePasswordSucceeded;
       changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
       if (!changePasswordSucceeded)
       {
           ModelState.AddModelError("PasswordMessage", "Ошибка! Данные введены не корректно");
           return View(model);
       }
       ModelState.AddModelError("PasswordMessage", "Пароль успешно изменен!");
     return View();
 }
示例#2
0
        public ActionResult Manage(LocalPasswordModel model)
        {
            GetCurrentUser();
            bool hasLocalAccount = OAuthWebSecurity.HasLocalAccount(WebSecurity.GetUserId(User.Identity.Name));
            ViewBag.HasLocalPassword = hasLocalAccount;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasLocalAccount)
            {
                if (ModelState.IsValid)
                {
                    // ChangePassword will throw an exception rather than return false in certain failure scenarios.
                    bool changePasswordSucceeded;
                    try
                    {
                        changePasswordSucceeded = WebSecurity.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword);
                        UsersContext db;
                        db = new UsersContext();
                        List<UserData> TempList = new List<UserData>();
                        TempList = db.UsersData.ToList();
                        ViewBag.Users = TempList;
                        UserData _model = new UserData();
                        for (int i = 0; i < TempList.Count; i++)
                        {
                            if (TempList[i].UserProfile.UserId == WebSecurity.CurrentUserId)
                            {
                                _model = TempList[i];
                                break;
                            }
                        }
                        ViewBag.currentUser = _model;
                    }
                    catch (Exception)
                    {
                        changePasswordSucceeded = false;
                    }

                    if (changePasswordSucceeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                    }
                }
            }
            else
            {
                // User does not have a local password so remove any validation errors caused by a missing
                // OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    try
                    {
                        WebSecurity.CreateAccount(User.Identity.Name, model.NewPassword);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError("", String.Format("Unable to create local account. An account with the name \"{0}\" may already exist.", User.Identity.Name));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
示例#3
0
 public ActionResult RecoverPasswordPage(LocalPasswordModel model)
 
 {
     if (model.NewPassword == null || model.ConfirmPassword == null)
     {
         ModelState.AddModelError("NewPassword", "Ошибка! Заполните все поля");
         return View(model);
     }
     if (model.NewPassword != model.ConfirmPassword)
     {
         ModelState.AddModelError("NewPassword", "Ошибка! Пароли должны совпадать");
         return View(model);
     }
     int s = WebSecurity.GetUserIdFromPasswordResetToken(model.OldPassword);
     string FindUser = "";
     WebSecurity.ResetPassword(model.OldPassword, model.NewPassword);
     UsersContext db = new UsersContext();
     List<UserProfile> _userProfileList = db.UserProfiles.ToList();
     for (int i = 0; i < _userProfileList.Count; i++)
     {
         if (_userProfileList[i].UserId == s)
         {
             FindUser = _userProfileList[i].UserName;
             break;
         }
     }
     bool logget = WebSecurity.Login(FindUser, model.NewPassword);
     if (logget)
         return RedirectToAction("Index", "Home");
     else 
         return View();
     
 }
示例#4
0
 public ActionResult RecoverPasswordPage(string Key)
 {
     LocalPasswordModel model = new LocalPasswordModel();
     model.OldPassword = Key;
     ViewBag.Key = Key;
     return View(model);
 }