コード例 #1
0
 static void Main(string[] args)
 {
     while (true)
     {
         CalendarContext db = new CalendarContext();
         Console.Clear();
         Console.WriteLine("Hi and Welcome to the Calendar-Planner Application!");
         Console.WriteLine();
         Console.WriteLine("Proceed by typing the corresponding number then");
         Console.WriteLine("press enter to execute the action:");
         Console.WriteLine("1: Log in to your account.");
         Console.WriteLine("2: Create new user.");
         Console.WriteLine("3: Remove existing user.");
         Console.WriteLine();
         Console.WriteLine("4: Close the application");
         string response = Console.ReadLine();
         if (response == "1")
         {
             PlannerMenu(ref db, UserLogin(ref db));
         }
         else if (response == "2")
         {
             PlannerMenu(ref db, UserCreation(ref db));
         }
         else if (response == "3")
         {
             UserDeletion(ref db);
         }
         else if (response == "4")
         {
             Environment.Exit(0);
         }
     }
 }
コード例 #2
0
 public static void UserRemove(ref CalendarContext db, string userName, string hashedPassword)
 {
     try
     {
         db.Users.Remove(new User
         {
             Username = userName,
             Password = hashedPassword,
         });
         db.SaveChanges();
         db.Calendars.Remove(new Calendar
         {
             CalendarId = db.GetCalendarId(userName)
         });
         db.SaveChanges();
     }
     catch (DbUpdateException)
     {
         Console.WriteLine("Username not found!");
         Console.WriteLine("Press enter to try again.");
         Console.ReadLine();
         db.DetachAllEntities();
         UserDeletion(ref db);
     }
 }
コード例 #3
0
        public static void PlanAdd(ref CalendarContext db, string userName)
        {
            bool add = true;

            while (add)
            {
                Console.Clear();
                Console.WriteLine("What are you planning to do?");
                string activity = Console.ReadLine();
                Console.WriteLine("Which date and time does it start?(dd/mm/yy hh:mm)");
                string startTime = Console.ReadLine();
                ValidateAddDateFormat(ref db, startTime, userName);
                Console.WriteLine("Until which date and time does it last?(dd/mm/yy hh:mm)");
                string endTime = Console.ReadLine();
                ValidateAddDateFormat(ref db, endTime, userName);
                db.AddPlanEntry(startTime, endTime, activity, userName);
                Console.WriteLine("Are you done adding plans?(y/n)");
                string response = Console.ReadLine();
                if (response == "n")
                {
                    add = true;
                    Console.Clear();
                }
                else if (response == "y")
                {
                    add = false;
                }
            }
        }
コード例 #4
0
        public static void UserGetHashedPassword(ref CalendarContext db, string userName, string passWord)
        {
            UserAuthentication(ref db, userName, passWord);
            string savedPasswordHash = db.Users.Where(u => u.Username == userName)
                                       .SingleOrDefault().Password;

            byte[] hashBytes = Convert.FromBase64String(savedPasswordHash);
            byte[] salt      = new byte[16];
            Array.Copy(hashBytes, 0, salt, 0, 16);
            var pbkdf2 = new Rfc2898DeriveBytes(passWord, salt, 10000);

            ConvertToHashedPassword(ref db, passWord, salt);
            byte[] hash = pbkdf2.GetBytes(20);
            for (int i = 0; i < 20; i++)
            {
                if (hashBytes[i + 16] == hash[i])
                {
                    UserAuthentication(ref db, userName, passWord);
                }
                else if (hashBytes[i + 16] != hash[i])
                {
                    Console.WriteLine("Password is incorrect!");
                    Console.WriteLine("Press enter to try again.");
                    Console.ReadLine();
                    UserLogin(ref db);
                    break;
                }
            }
        }
コード例 #5
0
        public static string UserCreation(ref CalendarContext db)
        {
            bool passwordConfirmation = true;

            while (passwordConfirmation)
            {
                Console.Clear();
                Console.WriteLine("Please enter your desired username:"******"Please enter your desired password:"******"Please confirm your desired password:"******"The password was not confirmed!");
                    Console.WriteLine("Press enter to try again.");
                    Console.ReadLine();
                    UserCreation(ref db);
                }
            }
            return(null);
        }
コード例 #6
0
        public static void PlanRemove(ref CalendarContext db, string userName)
        {
            bool remove = true;

            while (remove)
            {
                Console.Clear();
                Console.WriteLine("When is the plan you would like to remove?");
                string removeDate = Console.ReadLine();
                Console.WriteLine("Which plan would you like to remove?");
                string activity = Console.ReadLine();
                ValidateRemoveDateFormat(ref db, removeDate, activity, userName);
                db.RemovePlanEntry(removeDate, activity, userName);
                db.SaveChanges();
                Console.WriteLine("Are you done removing plans?(y/n)");
                string response = Console.ReadLine();
                if (response == "n")
                {
                    remove = true;
                    Console.Clear();
                }
                else if (response == "y")
                {
                    remove = false;
                }
            }
        }
コード例 #7
0
        public static string UserLogin(ref CalendarContext db)
        {
            Console.Clear();
            Console.WriteLine("Welcome! Please enter your username:"******"Please enter your password:");
            string passWord = UserPasswordMasking();

            UserGetHashedPassword(ref db, userName, passWord);
            return(userName);
        }
コード例 #8
0
        public static void ValidateAddDateFormat(ref CalendarContext db, string date, string userName)
        {
            bool addDateFormat = true;

            addDateFormat &= DateTime.TryParse(date, out DateTime result);
            if (!addDateFormat)
            {
                Console.WriteLine("This is not a valid date format, try again.");
                Console.ReadLine();
                PlanAdd(ref db, userName);
            }
        }
コード例 #9
0
 public static void ConvertToHashedPassword(ref CalendarContext db, string passWord, byte[] salt)
 {
     try
     {
         var pbkdf2 = new Rfc2898DeriveBytes(passWord, salt, 10000);
     }
     catch (Exception)
     {
         Console.WriteLine("Password is incorrect!");
         Console.WriteLine("Press enter to try again.");
         Console.ReadLine();
         UserLogin(ref db);
     }
 }
コード例 #10
0
        public static void ValidateRemoveDateFormat(ref CalendarContext db, string date, string activity, string userName)
        {
            bool removeDateFormat = true;
            bool planActivity     = true;

            removeDateFormat &= DateTime.TryParse(date, out DateTime result);
            planActivity     &= db.Plans.Any(x => x.Activity.ToUpper() == activity.ToUpper());
            if (!removeDateFormat)
            {
                Console.WriteLine("This is not a valid date format, try again.");
                Console.ReadLine();
                PlanRemove(ref db, userName);
            }
            else if (!planActivity)
            {
                Console.WriteLine("There is no such activity planned, try again.");
                Console.ReadLine();
                PlanRemove(ref db, userName);
            }
        }
コード例 #11
0
 public static void UserAdd(ref CalendarContext db, string userName, string hashedPassword)
 {
     try
     {
         db.Users.Add(new User {
             Username = userName,
             Password = hashedPassword,
             Calendar = new Calendar {
                 Username = userName
             }
         });
         db.SaveChanges();
     }
     catch (DbUpdateException)
     {
         Console.WriteLine("Username already taken!");
         Console.WriteLine("Press enter to try again.");
         Console.ReadLine();
         db.DetachAllEntities();
         UserCreation(ref db);
     }
 }
コード例 #12
0
        public static void UserDeletion(ref CalendarContext db)
        {
            bool passwordConfirmation = true;

            while (passwordConfirmation)
            {
                Console.Clear();
                Console.WriteLine("Please enter your username:"******"Please enter your password:"******"Please confirm your password:"******"This action will delete your account");
                    Console.WriteLine("Would you like to proceed? (y/n)");
                    string response = Console.ReadLine();
                    if (response == "y")
                    {
                        string hashedPassword = UserPasswordHashing(passWordConfirm);
                        UserRemove(ref db, userName, hashedPassword);
                        break;
                    }
                    else if (response == "n")
                    {
                        break;
                    }
                }
                else if (passWord != passWordConfirm)
                {
                    Console.WriteLine("The password was not confirmed!");
                    Console.WriteLine("Press enter to try again.");
                    Console.ReadLine();
                    passwordConfirmation = true;
                }
            }
        }
コード例 #13
0
 public static void UserAuthentication(ref CalendarContext db, string userName, string passWord)
 {
     try
     {
         string existingUser = db.Users.Single(u => u.Username == userName).ToString();
     }
     catch (InvalidOperationException)
     {
         Console.WriteLine("User does not exist!");
         Console.WriteLine("Press enter to try again.");
         Console.ReadLine();
         UserLogin(ref db);
     }
     catch (NullReferenceException)
     {
         Console.WriteLine("User does not exist!");
         Console.WriteLine("Press enter to try again.");
         Console.ReadLine();
         UserLogin(ref db);
     }
     string savedPasswordHash = db.Users.Where(u => u.Username == userName)
                                .SingleOrDefault().Password;
 }
コード例 #14
0
 public static void PlannerMenu(ref CalendarContext db, string userName)
 {
     while (true)
     {
         Console.Clear();
         Console.WriteLine("What would you like to do?");
         Console.WriteLine("Type the corresponding number then press enter to execute:");
         Console.WriteLine("1: Add Plans.");
         Console.WriteLine("2: Remove Plans.");
         Console.WriteLine("3: Display your existing calendar-planner.");
         Console.WriteLine("4: Log out.");
         Console.WriteLine();
         Console.WriteLine("5: Close the application.");
         string response = Console.ReadLine();
         if (response == "1")
         {
             PlanAdd(ref db, userName);
         }
         else if (response == "2")
         {
             PlanRemove(ref db, userName);
         }
         else if (response == "3")
         {
             db.DisplayPlans(userName);
         }
         else if (response == "4")
         {
             break;
         }
         else if (response == "5")
         {
             Environment.Exit(0);
         }
     }
 }