public ActionResult Delete(int id)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var user = context.FindSingle(UserSpecs.GetUser(id));

                if (HttpContext.Request.HttpMethod == "POST")
                {
                    try
                    {
                        context.Delete(user);
                        context.SaveChanges();

                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                    }
                }

                var roles = context
                            .Find(RoleSpecs.GetRoles())
                            .ToList();

                return(View(new UserDeleteViewModel(user, roles)));
            }
        }
        public ActionResult Edit(int id)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var user  = context.FindSingle(UserSpecs.GetUser(id));
                var roles = context
                            .Find(RoleSpecs.GetRoles())
                            .ToList();

                return(View(new UserEditViewModel(user, roles)));
            }
        }
 public void UpdatePassword(string username, string password)
 {
     using (var context = _contextFactory.CreateDataContext())
     {
         var user = context.FindSingle(UserSpecs.GetUser(username));
         if (user != null)
         {
             user.PasswordHash = SimpleHash.ComputeHash(password);
             context.SaveChanges();
         }
     }
 }
        public ApplicationUser Find(string username, string password)
        {
            using (var context = _contextFactory.CreateDataContext())
            {
                var user = context.FindSingle(UserSpecs.GetUser(username));
                if (user != null)
                {
                    if (SimpleHash.VerifyHash(password, user.PasswordHash))
                    {
                        return(new ApplicationUser(user.Username, user.Roles.Select(r => r.Role).ToArray()));
                    }
                }
            }

            return(null);
        }
        public ActionResult Edit(int id, UserEditEntryModel entryModel)
        {
            if (ModelState.IsValid)
            {
                using (var context = _contextFactory.CreateDataContext())
                {
                    try
                    {
                        var user = context.FindSingle(UserSpecs.GetUser(id));
                        if (entryModel.IsChangePassword)
                        {
                            user.PasswordHash = ApplicationUserManager.SimpleHash.ComputeHash(entryModel.TempPassword);
                        }

                        var userRolesToBeDeleted = user.Roles.Where(r => !entryModel.UserRoles.Contains(r.Role)).ToList();
                        foreach (var userRole in userRolesToBeDeleted)
                        {
                            user.Roles.Remove(userRole);
                        }

                        var userRolesToBeAdded = entryModel.UserRoles.Where(ur => !user.Roles.Any(r => r.Role == ur)).Select(ur => new Persistence.Entities.UserRole {
                            Role = ur
                        }).ToList();
                        foreach (var userRole in userRolesToBeAdded)
                        {
                            user.Roles.Add(userRole);
                        }

                        context.SaveChanges();

                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("", ex.Message);
                    }
                }
            }

            return(Edit(id));
        }