Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class Program : Utils
- {
- static bool _UserExist(string username)
- {
- Utils tools = new Utils();
- SqlConnection connection = new SqlConnection(tools.ConnectionString);
- SqlCommand command = new SqlCommand
- {
- Connection = connection,
- CommandType = CommandType.Text,
- CommandText = "USE TAO; SELECT * FROM [User] WHERE Username = " + "'" + username + "';"
- };
- try
- {
- connection.Open();
- var reader = command.ExecuteReader();
- if (reader.HasRows)
- return true;
- }
- catch(Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Error during the creation of the user : " + ex.Message);
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- return false;
- }
- static void _AddUser()
- {
- MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider();
- List<char> passwordMask = new List<char>();
- Utils tools = new Utils();
- string username = null, password = "";
- SqlConnection connection = new SqlConnection(tools.ConnectionString);
- SqlCommand command = new SqlCommand();
- Console.WriteLine("CreationUserTool\n");
- while (string.IsNullOrEmpty(username))
- {
- Console.Write("Username : ");
- username = Console.ReadLine();
- }
- while (passwordMask.Count == 0 || passwordMask.ToString() == "")
- {
- Console.Write("Password : ");
- ConsoleKeyInfo consoleKeyInfo;
- while ((consoleKeyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
- {
- if (consoleKeyInfo.Key != ConsoleKey.Backspace)
- {
- passwordMask.Add(consoleKeyInfo.KeyChar);
- Console.Write("*");
- }
- else if (passwordMask.Count > 0)
- {
- Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
- Console.Write(" ");
- Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
- if (passwordMask.Count > 0)
- passwordMask.RemoveAt(passwordMask.Count - 1);
- }
- }
- Console.WriteLine("");
- //password = Console.ReadLine();
- }
- if (_UserExist(username))
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("User already exist");
- Console.ForegroundColor = ConsoleColor.White;
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- foreach (var item in passwordMask)
- {
- password += item;
- }
- var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
- md5Hash.Dispose();
- command.Connection = connection;
- command.CommandType = CommandType.Text;
- command.CommandText = "USE TAO; INSERT into [User] (Id, Username, Password) VALUES (@Id, @Username, @Password);";
- command.Parameters.AddWithValue("@Id", Guid.NewGuid());
- command.Parameters.AddWithValue("@Username", username);
- command.Parameters.AddWithValue("@Password", Convert.ToBase64String(data));
- try
- {
- connection.Open();
- command.ExecuteNonQuery();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("User added successfully");
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- catch (SqlException ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Error during the creation of the user : " + ex.Message);
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- }
- static void _ModifyUser()
- {
- int i, choice;
- string username = null, password = "";
- List<string> users = new List<string>();
- Utils tools = new Utils();
- SqlConnection connection = new SqlConnection(tools.ConnectionString);
- SqlCommand command = new SqlCommand
- {
- Connection = connection,
- CommandType = CommandType.Text,
- CommandText = "SELECT Username FROM [User];"
- };
- try
- {
- connection.Open();
- var reader = command.ExecuteReader();
- while (reader.Read())
- {
- IDataRecord record = (IDataRecord)reader;
- users.Add($"{record[0]}");
- }
- reader.Close();
- if (users.Count == 0)
- {
- Console.WriteLine("ModificationUserTool\n");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("No user to update");
- Console.ForegroundColor = ConsoleColor.Gray;
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- while (users.Count > 0)
- {
- Console.WriteLine("ModificationUserTool\n");
- Console.WriteLine("Choose user for modification : \n");
- i = 1;
- foreach (var user in users)
- {
- Console.WriteLine($"\t{i}) {user}");
- i++;
- }
- Console.WriteLine($"\t{i}) Quit\n");
- Console.Write("Which user : ");
- if (Int32.TryParse(Console.ReadLine(), out choice))
- {
- if (choice - 1 >= 0 && choice - 1 < users.Count)
- {
- command.CommandText = "USE TAO; DELETE FROM [User] WHERE Username = '" + users[choice - 1] + "';";
- command.ExecuteNonQuery();
- users.RemoveAt(choice - 1);
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("User deleted successfully");
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- else if (choice - 1 == users.Count)
- {
- return;
- }
- }
- Console.Clear();
- }
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Error during the creation of the user : " + ex.Message);
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- }
- static void _DeleteUser()
- {
- List<string> users = new List<string>();
- Utils tools = new Utils();
- SqlConnection connection = new SqlConnection(tools.ConnectionString);
- SqlCommand command = new SqlCommand
- {
- Connection = connection,
- CommandType = CommandType.Text,
- CommandText = "SELECT Username FROM [User];"
- };
- try
- {
- connection.Open();
- var reader = command.ExecuteReader();
- while (reader.Read())
- {
- IDataRecord record = (IDataRecord)reader;
- users.Add($"{record[0]}");
- }
- reader.Close();
- if (users.Count == 0)
- {
- Console.WriteLine("ModificationUserTool\n");
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("No users to delete");
- Console.ForegroundColor = ConsoleColor.Gray;
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- while (users.Count > 0)
- {
- Console.WriteLine("DeletionUserTool\n");
- Console.WriteLine("Choose agent for deletion : \n");
- var i = 1;
- foreach (var user in users)
- {
- Console.WriteLine($"\t{0}) {1}", i, user);
- i++;
- }
- Console.WriteLine($"\t{i}) Quit\n");
- Console.Write("Which agent : ");
- int choice;
- if (Int32.TryParse(Console.ReadLine(), out choice))
- {
- if (choice - 1 >= 0 && choice - 1 < users.Count)
- {
- command.CommandText = "USE TAO; DELETE FROM [User] WHERE Username = '" + users[choice - 1] + "';";
- command.ExecuteNonQuery();
- users.RemoveAt(choice - 1);
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("User deleted successfully");
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- else if (choice - 1 == users.Count)
- {
- return;
- }
- }
- Console.Clear();
- }
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Error during the creation of the user : " + ex.Message);
- ConsoleKeyInfo key = Console.ReadKey();
- if (key.Key == ConsoleKey.Enter || key.Key != ConsoleKey.Enter)
- return;
- }
- finally
- {
- Console.ForegroundColor = ConsoleColor.Gray;
- }
- }
- static void CheckChoice(string choice)
- {
- Console.Clear();
- switch (choice)
- {
- case "1":
- _AddUser();
- return;
- case "2":
- _ModifyUser();
- return;
- case "3":
- _DeleteUser();
- return;
- }
- }
- static void Main(string[] args)
- {
- string choice;
- byte quit = 0;
- while (quit != 1)
- {
- do
- {
- Console.Clear();
- Console.WriteLine("Welcome to the UsersManagementTool\n");
- Console.WriteLine("This tool allows you to :\n");
- Console.WriteLine("\t1) Create an user.");
- Console.WriteLine("\t2) Modify an user.");
- Console.WriteLine("\t3) Delete an user.");
- Console.WriteLine("\t4) Quit.\n");
- Console.Write("What do you want ? ");
- choice = Console.ReadLine();
- } while (choice != "1" && choice != "2" && choice != "3" && choice != "4");
- if (choice == "4")
- quit = 1;
- else
- CheckChoice(choice);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement