public void AddUser(User user)
 {
     using (var db = new BlogContext())
     {
         foreach (Role r in user.Roles)
         {
             db.Roles.Attach(r);
         }
         db.Users.Add(user);
         db.SaveChanges();
     }
 }
        public ActionResult Login(User user, string returnUrl)
        {
            try
            {
                if (userManager.ValidateLogin(ref user))
                {
                    logger.Log("", LogType.Login, user.Username);

                    Session["Login"] = user;
                    Json(comManager.GetUnreadCommentsCount(((User)Session["Login"]).Id), JsonRequestBehavior.AllowGet);
                    return RedirectToAction("Home");
                }
                else
                {
                    if (user.Username == null || user.Password == null)
                    {
                        ModelState.AddModelError(string.Empty, "All fields are required");
                        return View();
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Username or password is invalid");
                        return View();
                    }
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Server Error, please try again later");
                return View();
            }
        }
 public void RemoveUser(User user)
 {
     using (var db = new BlogContext())
     {
         var query = (from u in db.Users
                      where u.IsActive == true && u.Id == user.Id
                      select u).FirstOrDefault<User>();
         query.IsActive = false;
         db.SaveChanges();
     }
 }
        public void ModifyUser(User user)
        {
            using (var db = new BlogContext())
            {
                var query = (from u in db.Users.Include(r=>r.Roles)
                             where u.IsActive == true && u.Id == user.Id
                             select u).FirstOrDefault<User>();

                var types = user.Roles.Select(r => r.Type);

                var newRoles = db.Roles
                              .Where(r => types.Contains(r.Type)).ToList();
                query.Email = user.Email;
                query.Name = user.Name;
                query.Surname = user.Surname;
                query.Password = user.Password;
                query.PicturePath = user.PicturePath;
                query.Roles.Clear();
                query.Roles.AddRange(newRoles);
                db.SaveChanges();
            }
        }