Ejemplo n.º 1
0
        public ActionResult AccountInfo(AccountInfoModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //get the currently logged in user
                    MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);

                    //if it is not the same Email as before
                    if (!currentUser.Email.Equals(model.Email))
                    {
                        //check to see if there is another user with the same email address 
                        //and throw an exception in case it does
                        var userName = Membership.GetUserNameByEmail(model.Email);
                        if (!String.IsNullOrEmpty(userName) && !currentUser.UserName.Equals(userName))
                            throw new ApplicationException("There's another user with the same Email address. Please provide a different Email address.");

                        //set the Email and update the user
                        currentUser.Email = model.Email;
                        Membership.UpdateUser(currentUser);
                    }

                    //set the Success message
                    TempData["PageMessage"] = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Success,
                        Message = "User " + User.Identity.Name + " has been updated successfully!"
                    };

                    return RedirectToAction("AccountInfo", "Account");
                }
                catch (Exception ex)
                {
                    //provide an error message in case of an Exception
                    model.PageMessage = new PageMessageModel
                    {
                        Type = PageMessageModel.MessageType.Error,
                        Message = ex.Message
                    };
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 2
0
 public ActionResult AccountInfo()
 {
     //get the currently logged in user and set the Email and UserName fields in the model
     //also get the message from the TampData object
     MembershipUser currentUser = Membership.GetUser(User.Identity.Name, userIsOnline: true);
     AccountInfoModel model = new AccountInfoModel()
     {
         Email = currentUser.Email,
         PageMessage = TempData["PageMessage"] as PageMessageModel ?? new PageMessageModel()
     };
     return View(model);
 }