Ejemplo n.º 1
0
        public ActionResult EditUser([Bind(Include = "ID,User_Type_ID,User_First_Name,User_Last_Name,User_Phone_Number,User_Email,User_ID,User_Password")] User user)
        {
            if (ModelState.IsValid)
            {
                user.User_Password_Salt = HashPasswordService.CreateSalt();

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

                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("UsersList"));
            }

            ViewBag.User_Type_ID = new SelectList(db.User_Type, "ID", "Type", user.User_Type_ID);
            return(View(user));
        }
Ejemplo n.º 2
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"));
            }
        }