示例#1
0
        public IActionResult addIdea(string itext)
        {
            int?UserId = HttpContext.Session.GetInt32("id");

            if (UserId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            if (itext == null)
            {
                TempData["ideaerror"] = "No ideas?";
            }
            Idea idea = new Idea {
                itext  = itext,
                userId = (int)UserId,
            };

            _context.Ideas.Add(idea);
            _context.SaveChanges();
            return(RedirectToAction("Main"));
        }
示例#2
0
 public IActionResult Register(Registration u)  //comes from ViewModel
 {
     if (ModelState.IsValid)
     {
         //check for unique email
         List <User> sameEmail = _context.Users.Where(dbu => dbu.email == u.email).ToList();
         if (sameEmail.Count == 0)
         {
             //create new instance of user class
             User newUser = new User {
                 fname = u.fname,
                 lname = u.lname,
                 email = u.email,
                 alias = u.alias
             };
             //hash pw
             PasswordHasher <User> Hasher = new PasswordHasher <User>();
             newUser.password = Hasher.HashPassword(newUser, u.password);
             //add user to bd
             _context.Users.Add(newUser);
             _context.SaveChanges();
             //set session
             HttpContext.Session.SetInt32("id", newUser.id);
             HttpContext.Session.SetString("username", newUser.fname);
             //success
             return(RedirectToAction("Main", "Idea", new { id = newUser.id }));
         }
         else
         {
             //show view with errors
             TempData["unique"] = "Email is already registered";
             return(RedirectToAction("Index"));
         }
     }
     return(View("Index"));
 }