public User CreateUser(User user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    BadRequest();
                }
                _dbContext.Users.Add(user);
                _dbContext.SaveChanges();

                return(user);
            }
            catch (Exception)
            {
                throw new Exception("Some message here");
            }
        }
        public ActionResult CreateCourse([Bind(Include = "CourseID,Course_Name,Course_Code,Course_Description,Course_Credit,Course_Instructor_ID,Course_Assistant_ID")] Course course)
        {
            try
            {
                ViewBag.Course_Assistant_ID  = new SelectList(db.Assistants, "ID", "Assistant_Title", course.Course_Assistant_ID);
                ViewBag.Course_Instructor_ID = new SelectList(db.Instructors, "ID", "Instructor_Title", course.Course_Instructor_ID);

                if (ModelState.IsValid)
                {
                    db.Courses.Add(course);
                    db.SaveChanges();
                    return(RedirectToAction("CoursesList"));
                }
                return(View(course));
            }

            catch (Exception e)
            {
                return(View("Error", e));
            }
        }
Exemple #3
0
        public ActionResult CreateUser([Bind(Include = "ID,User_Type_ID,User_First_Name,User_Last_Name,User_Phone_Number,User_Email,User_ID,User_Password")] User user)
        {
            try
            {
                ViewBag.User_Type_ID = new SelectList(db.User_Type, "ID", "Type", user.User_Type_ID);

                if (ModelState.IsValid)
                {
                    //using (SHA512 sha512Hash = SHA512.Create())
                    //{
                    //    // Generate unique salt for each user
                    //    user.User_Password_Salt = Crypto.GenerateSalt();

                    //    // From String to byte array + salt
                    //    byte[] sourceBytes = Encoding.UTF8.GetBytes(user.User_Password + user.User_Password_Salt);
                    //    byte[] hashBytes = sha512Hash.ComputeHash(sourceBytes);

                    //    // Converting hashed byte array back to string format
                    //    user.User_Password = BitConverter.ToString(hashBytes).Replace("-", String.Empty);
                    //}

                    user.User_Password_Salt = HashPasswordService.CreateSalt();

                    user.User_Password = HashPasswordService.CreateHash(user.User_Password, user.User_Password_Salt);

                    db.Users.Add(user);
                    db.SaveChanges();
                    return(RedirectToAction("UsersList"));
                }

                return(View(user));
            }

            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
                return(View("Error"));
            }
        }