public async Task<ActionResult> Edit(EditUserViewModel model)
        {
            var Db = DatabaseFactory.Get();

            if (ModelState.IsValid)
            {
                var user = Db.Users.First(u => u.Id == model.Id);
                string name = model.FirstName.Trim() + "." + model.LastName.Trim();
                // Update the user data:
                //user.FirstName = model.FirstName; //We can't have FirstName and
                //user.LastName = model.LastName;   //LastName without refactor
                user.UserName = name;
                user.LoweredUserName = name.ToLower();
                user.Email = model.Email.Trim();
                user.LoweredEmail = model.Email.Trim().ToLower();
                user.IsApproved = model.IsApproved;
                user.IsLockedOut = model.IsLockedOut;
                ModelState state = ModelState["NewPassword"];
                bool changePassword = state.Value.AttemptedValue == "" ? false : true;
                bool hasPassword = HasPassword(user);
                if (changePassword && hasPassword)
                {
                    IdentityResult remove = await UserManager.RemovePasswordAsync(user.Id);
                    if (remove.Succeeded)
                    {
                        IdentityResult result = await UserManager.AddPasswordAsync(user.Id, model.NewPassword);
                        if (result.Succeeded)
                        {
                            user.LastPasswordChangedDate = DateTime.Today.AddMonths(-PASSWORD_EXPIRATION_IN_MONTHS);
                            ViewBag.Message = "Password successfully updated.";
                        }
                        else
                        {
                            throw new MacheteIntegrityException("HELL to the no. You have to ADD a password if you remove one.");
                        }
                    }
                    else
                    {
                        model.ErrorMessage = "Something went wrong with your password request. We should really learn to test our software. Sorry!";
                        model.NewPassword = "";
                        model.ConfirmPassword = "";
                        return View(model);
                    }
                }
                else if (changePassword && !hasPassword)
                {
                    model.ErrorMessage = "This user's password is managed by another service.";
                    model.NewPassword = "";
                    model.ConfirmPassword = "";
                    return View(model);
                }
                Db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                await Db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            // If we got this far, something failed, redisplay form
            model.ErrorMessage = "Passwords must match.";
            model.NewPassword = "";
            model.ConfirmPassword = "";
            return View(model);
        }
 public ActionResult Delete(string id = null)
 {
     var Db = DatabaseFactory.Get();
     var user = Db.Users.First(u => u.Id == id);
     var model = new EditUserViewModel(user);
     if (user == null)
     {
         return HttpNotFound();
     }
     return View(model);
 }
        public ActionResult Edit(string id, ManageMessageId? Message = null)
        {
            ApplicationUser user = DatabaseFactory.Get().Users.First(u => u.Id == id);
            if (user == null)
            {
                return HttpNotFound();
            }

            EditUserViewModel model = new EditUserViewModel(user);

            return View(model);
        }