コード例 #1
0
ファイル: AccountController.cs プロジェクト: felipecsl/dover
        public ActionResult Profile(FormCollection _vars)
        {
            var user = Membership.GetUser(Guid.Parse(_vars["UserGuid"]));
            var prfMgr = new UserProfileManager(user);

            UserProfileViewModel viewModel = new UserProfileViewModel() {
                AccountProperties = new UserProfileManager(user).UserProfile.Properties,
                UserId = (Guid)user.ProviderUserKey,
                Email = user.Email,
            };

            user.Email = _vars["Email"];

            string currPwd = _vars["CurrentPassword"];
            string newPwd = _vars["NewPassword"];
            string newPwdConfirmation = _vars["NewPasswordConfirmation"];

            if (!string.IsNullOrWhiteSpace(currPwd)) {
                // attempt to change the user's password
                if (string.IsNullOrWhiteSpace(newPwd)) {
                    ModelState.AddModelError("NewPassword", "Você deve digitar a nova senha.");
                    return View(viewModel);
                }
                if (string.IsNullOrWhiteSpace(newPwdConfirmation)) {
                    ModelState.AddModelError("NewPassword", "Você deve digitar a confirmação da nova senha.");
                    return View(viewModel);
                }
                if (newPwdConfirmation != newPwd) {
                    ModelState.AddModelError("NewPassword", "A nova senha e a confirmação devem ser iguais.");
                    ModelState.AddModelError("NewPasswordConfirmation", "A nova senha e a confirmação devem ser iguais.");
                    return View(viewModel);
                }

                if (!user.ChangePassword(currPwd, newPwd)) {
                    ModelState.AddModelError("CurrentPassword", "Não foi possível trocar sua senha. Verifique sua senha atual e tente novamente.");
                    return View(viewModel);
                }
            }

            if (_vars["Administrator"] != null) {
                if (!Roles.IsUserInRole(user.UserName, "administrators")) {
                    Roles.AddUserToRole(user.UserName, "administrators");
                }
            }
            else if (Roles.IsUserInRole(user.UserName, "administrators")) {
                Roles.RemoveUserFromRole(user.UserName, "administrators");
            }

            _vars.Remove("UserGuid");
            _vars.Remove("Email");
            _vars.Remove("CurrentPassword");
            _vars.Remove("NewPassword");
            _vars.Remove("NewPasswordConfirmation");
            _vars.Remove("Administrator");

            foreach (string key in _vars.Keys) {
                prfMgr.SetUserProfileProperty(key, _vars[key]);
            }

            try {
                Membership.UpdateUser(user);
            }
            catch (ProviderException e) {
                ModelState.AddModelError("Email", "Ocorreu um erro atualizar seus dados. " + e.Message);
                return View(viewModel);
            }

            TempData["Message"] = "Dados salvos com sucesso.";
            return RedirectToAction("Index", "Home");
        }
コード例 #2
0
ファイル: AccountController.cs プロジェクト: felipecsl/dover
        public ActionResult Profile()
        {
            MembershipUser user = Membership.GetUser();

            if (user == null) {
                RedirectToAction("Login");
            }

            UserProfileViewModel viewModel = new UserProfileViewModel() {
                AccountProperties = new UserProfileManager(user).UserProfile.Properties,
                UserId = (Guid)user.ProviderUserKey,
                Email = user.Email,
            };

            return View(viewModel);
        }