public void LogOut(HttpSession session)
        {
            using (var context = new NotesApplicationContext())
            {
                Login login = context.Logins.Where(s => s.SessionId == session.Id).FirstOrDefault();

                context.Logins.Remove(login);
                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
 public IActionResult <UserProfileViewModel> Profile(AddNoteBindingModel model)
 {
     using (var context = new NotesApplicationContext())
     {
         User user = context.Users.Find(model.UserId);
         Note note = new Note()
         {
             Title   = model.Title,
             Content = model.Content
         };
         user.Notes.Add(note);
         context.SaveChanges();
     }
     return(Profile(model.UserId));
 }
        public IActionResult Register(RegisterUserBindingModel model)
        {
            var user = new User()
            {
                Username = model.Username,
                Password = model.Password
            };

            using (var context = new NotesApplicationContext())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }

            return(View());
        }
        public IActionResult Login(LoginUserBindingModel model, HttpSession session)
        {
            string username     = model.Username;
            string password     = model.Password;
            string sessionId    = session.Id;
            bool   loginSuccess = false;

            // Validate such user with password exist in db.
            using (var context = new NotesApplicationContext())
            {
                var user = context.Users.Where(u => (u.Username == username && u.Password == password)).FirstOrDefault();

                if (user != null)
                {
                    loginSuccess = true;

                    Login login = new Login()
                    {
                        SessionId = sessionId,
                        User      = user,
                        IsActive  = true
                    };

                    // Add login to db.
                    context.Logins.Add(login);
                    context.SaveChanges();
                }
            }

            if (loginSuccess == true)
            {
                return(Redirect("../home/index"));
            }

            return(View());
        }
Ejemplo n.º 5
0
        public IActionResult Login(LoginUserBindingModel model, HttpSession session, HttpResponse response)
        {
            string username  = model.Username;
            string password  = model.Password;
            string sessionId = session.Id;

            using (var context = new NotesApplicationContext())
            {
                User user = context.Users.FirstOrDefault(u => u.Username == username && u.Password == password);
                if (user != null)
                {
                    Login login = new Login()
                    {
                        User = user, SessionId = sessionId
                    };
                    context.Logins.Add(login);
                    context.SaveChanges();
                    Redirect(response, "/home/index");
                    return(null);
                }
            }

            return(this.View());
        }