Ejemplo n.º 1
0
        public IActionResult RegisterUser(RegisterUser newuser)
        {
            User CheckEmail = _dContext.users
                              .Where(u => u.email == newuser.email)
                              .SingleOrDefault();

            if (CheckEmail != null)
            {
                ViewBag.errors = "That email already exists";
                return(RedirectToAction("Register"));
            }
            if (ModelState.IsValid)
            {
                PasswordHasher <RegisterUser> Hasher = new PasswordHasher <RegisterUser>();
                User newUser = new User
                {
                    user_id    = newuser.user_id,
                    first_name = newuser.first_name,
                    last_name  = newuser.last_name,
                    email      = newuser.email,
                    address    = newuser.address,
                    city       = newuser.city,
                    state      = newuser.state,
                    zip        = newuser.zip,
                    phone      = newuser.phone,
                    password   = Hasher.HashPassword(newuser, newuser.password)
                };
                _dContext.Add(newUser);
                _dContext.SaveChanges();
                ViewBag.success = "Successfully registered";
                return(RedirectToAction("Login"));
            }
            else
            {
                return(View("Register"));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Register(ValidateViewModel model)
        {
            List <Person> AllUsers = _context.User.ToList();
            string        level    = "";

            if (AllUsers.Count == 0)
            {
                level = "Admin";
            }
            else
            {
                level = "Normal";
            }
            if (ModelState.IsValid)
            {
                Person newUser = new Person
                {
                    firstName  = model.firstName,
                    lastName   = model.lastName,
                    email      = model.email,
                    user_level = level,
                    created_at = DateTime.Now,
                    updated_at = DateTime.Now
                };
                PasswordHasher <Person> hasher = new PasswordHasher <Person>();
                newUser.password = hasher.HashPassword(newUser, model.password);
                _context.Add(newUser);
                _context.SaveChanges();
                HttpContext.Session.SetInt32("currentUserId", newUser.id);
                return(RedirectToAction("Dash"));
            }
            else
            {
                return(View(model));
            }
        }