Membership.UpdateUser(用户)不会同时更新密码和其他选项

时间:2012-11-23 16:54:50

标签: asp.net asp.net-mvc asp.net-mvc-4

Mates,我在这里遇到了一些逻辑问题。

如果我使用任何其他设置将IsApproved设置为true / false,则会在db中更新其值。

Altough,当我设置一个新密码时,我正在更改的所有其他设置都没有在服务器中更新。

你能帮助我吗?

CONTROLLER

public ActionResult EditUser(EditModel model)
    {
        if (ModelState.IsValid)
        {

            try
            {
                MembershipUser user = Membership.GetUser(model.UserName);

                user.IsApproved = bool.Parse(Request.Form.GetValues("IsApproved")[0]);

                if (model.PasswordAccount != null)
                    user.ChangePassword(model.PasswordAccount, model.NewPassword);


                if (model.PasswordQuestion != null)
                    user.ChangePasswordQuestionAndAnswer(model.CurrentPass, model.PasswordQuestion, model.PasswordAnwser);   


                if (model.Email != null)
                {
                    bool emailExist = CheckEmail(model.Email);
                    if (emailExist == false)
                    {
                        user.Email = model.Email;
                    }
                }

                Membership.UpdateUser(user);

                return Content("Usuário Atualizado com Sucesso!");

            }
            catch (Exception e)
            {
                return Content("Usuário não atualizado - Erro: " + e);
            }

        }
        else
        {
            return Content("Model Inválido");
        }

    }

我没有得到错误并通过调试检查我没有得到anu错误......

1 个答案:

答案 0 :(得分:0)

我很确定这不是最好的方式,但它正在发挥作用,直到我找到更好的解决方案,这是有效的:

try
            {
                MembershipUser user = Membership.GetUser(model.UserName);

                user.IsApproved = bool.Parse(Request.Form.GetValues("IsApproved")[0]);

                if (model.Email != null)
                {
                    bool emailExist = CheckEmail(model.Email);
                    if (emailExist == false)
                    {
                        user.Email = model.Email;
                    }
                }

                Membership.UpdateUser(user);

                user = Membership.GetUser(model.UserName);

                if (model.PasswordAccount != null)
                    user.ChangePassword(model.PasswordAccount, model.NewPassword);


                if (model.PasswordQuestion != null)
                    user.ChangePasswordQuestionAndAnswer(model.CurrentPass, model.PasswordQuestion, model.PasswordAnwser);

                Membership.UpdateUser(user);

                return Content("Usuário Atualizado com Sucesso!");

            }