public void AddMessage(ApplicationUser currentUser,string receiverId,string message)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var receiver = manager.FindById(receiverId);
            var newMessage = new UserMessage();

            newMessage.DateAndTime = DateTime.Now;
            newMessage.Message = message;
            newMessage.SenderId=currentUser.Id;
            newMessage.SenderFirstName = currentUser.FirstName;
            newMessage.SenderLastName = currentUser.LastName;
            newMessage.ReceiverId = receiver.Id;

            context.UserMessages.Add(newMessage);
            context.SaveChanges();
        }
        public void AddMessage(ApplicationUser currentUser, string receiverId, HttpPostedFileBase image)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var receiver = manager.FindById(receiverId);
            var newMessage = new UserMessage();

            newMessage.DateAndTime = DateTime.Now;
            newMessage.ImageData = new byte[image.ContentLength];
            newMessage.ImageMimeType = image.ContentType;
            image.InputStream.Read(newMessage.ImageData, 0, image.ContentLength);
            newMessage.SenderId = currentUser.Id;
            newMessage.SenderFirstName = currentUser.FirstName;
            newMessage.SenderLastName = currentUser.LastName;
            newMessage.ReceiverId = receiver.Id;

            context.UserMessages.Add(newMessage);
            context.SaveChanges();
        }
        public ActionResult AddUser(RegisterViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName };

                var result =manager.Create(user, model.Password);

                if (result.Succeeded)
                {
                    TempData["InfoMessage"]=" ";
                    TempData["SuccessMessage"] = "User successfully created";

                    return RedirectToAction("EditUsers");
                }

            }
            TempData["InfoMessage"] = "Such email is used";

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public bool AddToFriends(ApplicationUser currentUser,string id)
        {
            UserFriend newFriend = new UserFriend();

            newFriend.FriendId = id;
            newFriend.UserId = currentUser.Id;

            context.UserFriends.Add(newFriend);
            context.SaveChanges();

            return true;
        }
        public void UpdateProfile(ApplicationUser currentUser)
        {
            var oldUser = context.Users.Find(currentUser.Id);

            oldUser.About = currentUser.About;
            oldUser.Address = currentUser.Address;
            oldUser.Age = currentUser.Age;
            oldUser.City = currentUser.City;
            oldUser.Country = currentUser.Country;
            oldUser.Sex = currentUser.Sex;
            oldUser.ImageMimeType = currentUser.ImageMimeType;
            oldUser.ImageData = currentUser.ImageData;

            context.SaveChanges();
        }
        public bool RemoveFromFriends(ApplicationUser currentUSer, string email)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
            var delete = manager.FindByEmail(email);

            var id = context.UserFriends.First(u => u.UserId == currentUSer.Id && u.FriendId == delete.Id);

            context.UserFriends.Remove(id);
            context.SaveChanges();

            return true;
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName=model.FirstName, LastName=model.LastName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("ViewProfile", "Profile", new { id = User.Identity.GetUserId() });
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
 }