Example #1
0
        public IActionResult Register(User user)
        {
            if (ModelState.IsValid)
            {
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    ModelState.AddModelError("Email", "This Email already exist");
                    return(View("Index"));
                }

                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                user.Password = Hasher.HashPassword(user, user.Password);

                dbContext.Add(user);
                dbContext.SaveChanges();

                HttpContext.Session.SetString("email", user.Email);

                return(RedirectToAction("Success"));
            }
            else
            {
                return(View("Index"));
            }
        }
Example #2
0
 public IActionResult CreateWedding(Wedding newWedding)
 {
     if (ModelState.IsValid)
     {
         int?userId = HttpContext.Session.GetInt32("UserId");
         newWedding.UserId = (int)userId;
         dbContext.Add(newWedding);
         dbContext.SaveChanges();
         return(RedirectToAction("ViewWedding", new { weddingId = newWedding.WeddingId }));
     }
     else
     {
         return(View("AddWedding"));
     }
 }
Example #3
0
 public IActionResult Register(User newUser)
 {
     if (ModelState.IsValid)
     {
         // If a User exists with provided email
         if (dbContext.Users.Any(u => u.Email == newUser.Email))
         {
             // Manually add a ModelState error to the Email field
             ModelState.AddModelError("Email", "Email already in use!");
             return(View("Index"));
         }
         // Initializing a PasswordHasher object, providing our User class as its type
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         newUser.Password = Hasher.HashPassword(newUser, newUser.Password);
         dbContext.Users.Add(newUser);
         //Save your user object to the database
         dbContext.SaveChanges();
         // creating a session to stay logged in
         HttpContext.Session.SetInt32("UserId", newUser.UserId);
         return(RedirectToAction("Success"));
     }
     else
     {
         return(View("Index"));
     }
 }
Example #4
0
        public IActionResult Register(UserObject user)
        {
            // Check initial ModelState
            if (ModelState.IsValid)
            {
                // If a User exists with provided email
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    // Manually add a ModelState error to the Email field, with provided error message
                    ModelState.AddModelError("Email", "Email already in use!");

                    // You may consider returning to the View at this point
                    return(View("Index", user));
                }
                else
                {
                    // Initializing a PasswordHasher object, providing our User class as its
                    PasswordHasher <UserObject> Hasher = new PasswordHasher <UserObject>();
                    user.Password = Hasher.HashPassword(user, user.Password);
                    //Save your user object to the database
                    dbContext.Add(user);
                    dbContext.SaveChanges();
                    HttpContext.Session.SetInt32("userid", user.UserId);
                    return(Redirect("/success")); //This doesn't exist yet
                }
            }
            // other code
            else
            {
                return(View("Index", user));
            }
        }
Example #5
0
        public IActionResult Create(NewUser user)
        {
            if (_context.users.Where(u => u.EmailAddress == user.EmailAddress).ToList().Count() > 0)
            {
                ModelState.AddModelError("EmailAddress", "Email already exists!");
                return(View("Index"));
            }

            if (ModelState.IsValid)
            {
                PasswordHasher <User> hasher = new PasswordHasher <User>();
                user.Password = hasher.HashPassword(user, user.Password);

                User ToCreate = new User()
                {
                    FirstName    = user.FirstName,
                    LastName     = user.LastName,
                    EmailAddress = user.EmailAddress,
                    Password     = hasher.HashPassword(user, user.Password)
                };
                _context.users.Add(ToCreate);
                _context.SaveChanges();

                HttpContext.Session.SetInt32("id", (int)ToCreate.User_Id);
                return(Json(user));
            }
            return(View("Index"));
        }
Example #6
0
        public IActionResult Register(ViewUser newuser)
        {
            if (ModelState.IsValid && _context.users.Where(u => u.email == newuser.email).ToList().Count == 0)
            {
                PasswordHasher <ViewUser> Hasher = new PasswordHasher <ViewUser>();
                newuser.password = Hasher.HashPassword(newuser, newuser.password);

                User insertuser = new User();

                insertuser.first_name = newuser.first_name;
                insertuser.last_name  = newuser.last_name;
                insertuser.email      = newuser.email;
                insertuser.password   = newuser.password;

                _context.users.Add(insertuser);
                _context.SaveChanges();

                TempData["registrationsuccess"] = "Registration successful!";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["registrationfailed"] = "Registration failed.";
                return(View("Index"));
            }
        }
        public IActionResult AddMessage(Message newMessage)
        {
            int?userId = HttpContext.Session.GetInt32("UserId");

            if (userId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                newMessage.UserId = (int)userId;
                dbContext.Add(newMessage);
                dbContext.SaveChanges();
                return(RedirectToAction("Dashboard"));
            }
            return(View("Dashboard"));
        }
Example #8
0
 public IActionResult AddPet(Pet pet)
 {
     if (HttpContext.Session.GetInt32("User") == null)
     {
         return(RedirectToAction("Index", "LoginReg"));
     }
     if (_context.pets.Where(i => i.image == pet.image).ToList().Count() == 0 && ModelState.IsValid)
     {
         pet.created_at = DateTime.Now;
         pet.posted_by  = (int)HttpContext.Session.GetInt32("User");
         _context.pets.Add(pet);
         _context.SaveChanges();
         return(RedirectToAction("Success"));
     }
     else
     {
         return(RedirectToAction("Success"));
     }
 }
        public IActionResult CreateWedding(Wedding newWedding)
        {
            int?userId = HttpContext.Session.GetInt32("UserId");

            if (userId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                var oneUser = dbContext.Users.FirstOrDefault(w => w.UserId == userId);
                newWedding.CreatorName = oneUser.FirstName;
                newWedding.UserId      = (int)userId;
                dbContext.Add(newWedding);
                dbContext.SaveChanges();
                return(RedirectToAction("Dashboard"));
            }

            return(View("AddWedding"));
        }
Example #10
0
        public IActionResult Register(User newUser)
        {
            if (ModelState.IsValid)
            {
                var userInDb = dbContext.Users.FirstOrDefault(u => u.Email == newUser.Email);
                if (userInDb != null)
                {
                    ModelState.AddModelError("Email", "This email already taken");
                    return(View("Index"));
                }

                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                newUser.Password = Hasher.HashPassword(newUser, newUser.Password);
                dbContext.Add(newUser);
                dbContext.SaveChanges();
                var userToLogIn = dbContext.Users.FirstOrDefault(u => u.Email == newUser.Email);
                HttpContext.Session.SetInt32("UserId", userToLogIn.UserId);
                return(RedirectToAction("Success"));
            }
            else
            {
                return(View("Index"));
            }
        }
Example #11
0
 public IActionResult CreateUser(User user)
 {
     if (ModelState.IsValid)
     {
         if (db.Users.Any(u => u.Email == user.Email))
         {
             ModelState.AddModelError("Email", "Email already in use!");
             return(View("Index"));
         }
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         user.Password = Hasher.HashPassword(user, user.Password);
         db.Add(user);
         db.SaveChanges();
         return(RedirectToAction("LoginPage"));
     }
     return(View("Index"));
 }
        public IActionResult Register(User newUser)
        {
            if (dbContext.Users.Any(u => u.Email == newUser.Email))
            {
                ModelState.AddModelError("Email", "That Email already exists!");
            }

            if (ModelState.IsValid)
            {
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                newUser.Password = Hasher.HashPassword(newUser, newUser.Password);
                HttpContext.Session.SetInt32("ID", newUser.Id);
                dbContext.Add(newUser);
                dbContext.SaveChanges();

                return(RedirectToAction("Success"));
            }
            return(View("Index"));
        }
Example #13
0
 public IActionResult Register(User newUser)
 {
     if (ModelState.IsValid)
     {
         if (db.Users.Any(u => u.Email == newUser.Email))
         {
             ModelState.AddModelError("Email", "Email already registered, please login.");
             return(View("Index", newUser));
         }
         PasswordHasher <User> Hasher = new PasswordHasher <User>();
         newUser.Password = Hasher.HashPassword(newUser, newUser.Password);
         db.Users.Add(newUser);
         db.SaveChanges();
         HttpContext.Session.SetInt32("userId", newUser.UserId);
         HttpContext.Session.SetString("userFirstName", newUser.FirstName);
         return(RedirectToAction("Success"));
     }
     return(View("Index", newUser));
 }
        public IActionResult AddCategory(Category newCategory)
        {
            int?userId = HttpContext.Session.GetInt32("UserId");

            if (userId == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                newCategory.CreatorId = (int)userId;
                dbContext.Add(newCategory);
                dbContext.SaveChanges();
                return(RedirectToAction("CreateCategory"));
            }
            else
            {
                List <Category> categories = dbContext.Categories
                                             .Where(category => category.CreatorId == userId).ToList();
                ViewBag.Categories = categories;
                return(View("CreateCategory"));
            }
        }
        public IActionResult Register(LoginRegFormModel userVM)
        {
            if (TryValidateModel(userVM.registerVM))
            {
                // model validated correctly --> success
                // confirm that a user does not exist with the selected username
                try
                {
                    // Dapper connection commands
                    // User testUser = userFactory.FindByUsername(userVM.registerVM.Username);

                    // Entity PostGres Code First command
                    User testUser = _context.Users.SingleOrDefault(user => user.Username == userVM.registerVM.Username);
                    if (testUser != null)
                    {
                        // the username currently exists in the database
                        string key          = "Username";
                        string errorMessage = "This username already exists. Please select another or login.";
                        ModelState.AddModelError(key, errorMessage);
                        TempData["errors"] = true;
                        return(RedirectToAction("Index"));
                    }
                }
                catch
                {
                    // if username was not found - do nothing and proceed
                }
                // confirm that a user does not exist with the selected email
                try
                {
                    // Dapper connection commands
                    // User testUser = userFactory.FindByEmail(userVM.registerVM.Email);

                    // Entity PostGres Code First command
                    User testUser = _context.Users.SingleOrDefault(user => user.Email == userVM.registerVM.Email);
                    if (testUser != null)
                    {
                        // the email currently exists in the database
                        string key          = "Email";
                        string errorMessage = "This email address already exists. Please select another or login.";
                        ModelState.AddModelError(key, errorMessage);
                        TempData["errors"] = true;
                        return(RedirectToAction("Index"));
                    }
                }
                catch
                {
                    // if email was not found - do nothing and proceed
                }
                // Dapper factory command
                // userFactory.Add(userVM.registerVM);

                // Entity PostGres Code First command
                User NewUser = new User(userVM.registerVM);

                // generate a 128-bit salt using a secure PRNG
                byte[] newSalt = new byte[128 / 8];
                using (var rng = RandomNumberGenerator.Create())
                {
                    rng.GetBytes(newSalt);
                }
                string newSaltString = Convert.ToBase64String(newSalt);
                NewUser.Salt = newSaltString;
                // hash password
                string SaltedPasswd          = NewUser.Password + newSaltString;
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                NewUser.Password = Hasher.HashPassword(NewUser, SaltedPasswd);

                _context.Users.Add(NewUser);
                _context.SaveChanges();
                string userSerialized = JsonConvert.SerializeObject(userVM.registerVM);
                TempData["user"] = (string)userSerialized;

                // store user id, first name, and username in session
                // run query to gather id number generated by the database
                // Dapper connection command
                // User NewUser = userFactory.FindByUsername(userVM.registerVM.Username);

                // Entity PostGres Code First command
                User UserFromDb = _context.Users.SingleOrDefault(user => user.Username == userVM.registerVM.Username);

                // login to the application
                HttpContext.Session.SetInt32(LOGGED_IN_ID, UserFromDb.UserId);
                HttpContext.Session.SetString(LOGGED_IN_USERNAME, UserFromDb.Username);
                HttpContext.Session.SetString(LOGGED_IN_FIRSTNAME, UserFromDb.FirstName);
                return(RedirectToAction("Success"));
            }
            // model did not validate correctly --> show errors to user
            TempData["errors"] = true;
            return(RedirectToAction("Index"));
        }