Advertisement
DKuzin

Untitled

Apr 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.95 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using MySql.Data.MySqlClient;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.IO;
  10. using System.Runtime;
  11. namespace WindowsFormsApplication6
  12. {
  13.     public partial class Login : Form
  14.     {
  15.         //Creating a string used to show the name of the logged in user
  16.         public static string currentuser;
  17.  
  18.         //Creating string used to connect to our database
  19.         private MySqlConnection conn;
  20.         private string server;
  21.         private string database;
  22.         private string uid;
  23.         private string password;
  24.  
  25.         public Login()
  26.         {
  27.             //Defining our logindata to be able to establish a connection between the program and our database
  28.             server = "localhost";
  29.             database = "mtgworkshop";
  30.             uid = "root";
  31.             password = "";
  32.  
  33.             //Assigning a string that defines all of our login data
  34.             string connString;
  35.             connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password}";
  36.  
  37.             //Makes it as a string that performs a new MySqlConnection
  38.             conn = new MySqlConnection(connString);
  39.  
  40.             InitializeComponent();
  41.         }
  42.         //Button to go to account creation
  43.         private void button2_Click(object sender, EventArgs e)
  44.         {
  45.             panel2.BringToFront();
  46.             panel2.Visible = true;
  47.         }
  48.         //Button to create an account
  49.         private void button3_Click(object sender, EventArgs e)
  50.         {
  51.             //Strings used to check if requirements has been passed
  52.             string user = textBox3.Text;
  53.             string pass = textBox4.Text;
  54.             string firstname = textBox6.Text;
  55.             string lastname = textBox5.Text;
  56.  
  57.             //Check if password box and re-type password box matches
  58.             if (pass != textBox7.Text)
  59.             {
  60.                 MessageBox.Show($"Password doesn't match!");
  61.             }
  62.             //Check if any fields has more than 50 characters, which is not allowed
  63.             else if (user.Length > 50 || pass.Length > 50 || lastname.Length > 50 || firstname.Length > 50 || textBox7.Text.Length > 50)
  64.             {
  65.                 MessageBox.Show($"At least one of the fields includes over 50 characters!");
  66.             }
  67.             //Makes sure that username text length is above 4 characters
  68.             else if (user.Length < 4)
  69.             {
  70.                 MessageBox.Show($"Username has to be at least 4 characters long!");
  71.             }
  72.             //Makes sure that username text length is above 8 characters
  73.             else if (pass.Length < 8)
  74.             {
  75.                 MessageBox.Show($"Password has to be minimal 8 characters long!");
  76.             }
  77.             //Runs a bool to check whether or not the username has been taken
  78.             else if (UserExist(user))
  79.             {
  80.                 MessageBox.Show($"Username already exists");
  81.             }
  82.             //If everything else returns true we will create the new accoount
  83.             else
  84.             {
  85.                 //Runs our Register code, where we insert our input to the database
  86.                 if (Register(user, pass, firstname, lastname))
  87.                 {
  88.                     MessageBox.Show($"User {user} has been created!");
  89.                     textBox3.Text = "";
  90.                     textBox4.Text = "";
  91.                     textBox5.Text = "";
  92.                     textBox6.Text = "";
  93.                     textBox7.Text = "";
  94.                     panel2.SendToBack();
  95.                     panel2.Visible = false;
  96.                 }
  97.                 //If an error occurs, we will notify the user
  98.                 else
  99.                 {
  100.                     MessageBox.Show($"Error creating {user}. Please try again!");
  101.                 }
  102.             }
  103.         }
  104.         //Button to go back to account login
  105.         private void button4_Click(object sender, EventArgs e)
  106.         {
  107.             panel2.SendToBack();
  108.             panel2.Visible = false;
  109.         }
  110.         //Button to login
  111.         private void button1_Click(object sender, EventArgs e)
  112.         {
  113.             //Strings used to check the logininfomation
  114.             string user = textBox1.Text;
  115.             string pass = textBox2.Text;
  116.  
  117.             //We run our login code to check whether or not the input match anyting in our database
  118.             if (IsLogin(user, pass))
  119.             {
  120.                 //We set the current user global variable
  121.                 currentuser = user;
  122.  
  123.                 //We redirect to the main menu
  124.                 textBox1.Text = "";
  125.                 textBox2.Text = "";
  126.                 MessageBox.Show($"Welcome back {user}!");
  127.                 WindowsFormsApplication6.Mainmenu f1 = new WindowsFormsApplication6.Mainmenu();
  128.                 f1.RefToLogin = this;
  129.                 this.Visible = false;
  130.                 f1.Show();
  131.                
  132.             }
  133.             else
  134.             {
  135.                 MessageBox.Show($"Username and password does not match.");
  136.             }
  137.         }
  138.         //Function used to connect with mySQL database and perform a register formula
  139.         public bool Register(string user, string pass, string firstname, string lastname)
  140.         {
  141.             string query = $"INSERT INTO users (id, username, password, firstname, lastname) VALUES('', '{user}', sha1('{pass}'),'{firstname}','{lastname}')";
  142.             MySqlCommand cmd = new MySqlCommand(query, conn);
  143.             conn.Open();
  144.             cmd.ExecuteNonQuery();
  145.             return true;
  146.         }
  147.         //Function used to check whether or not username is taken
  148.         public bool UserExist(string user)
  149.         {
  150.             string query = $"SELECT * FROM users WHERE username = '{user}';";
  151.  
  152.             try
  153.             {
  154.                 if (OpenConnection())
  155.                 {
  156.                     MySqlCommand cmd = new MySqlCommand(query, conn);
  157.                     MySqlDataReader reader = cmd.ExecuteReader();
  158.  
  159.                     if (reader.Read())
  160.                     {
  161.                         reader.Close();
  162.                         conn.Close();
  163.                         return true;
  164.                     }
  165.                     else
  166.                     {
  167.                         reader.Close();
  168.                         conn.Close();
  169.                         return false;
  170.                     }
  171.                 }
  172.                 else
  173.                 {
  174.                     conn.Close();
  175.                     return false;
  176.                 }
  177.             }
  178.             catch (Exception ex)
  179.             {
  180.                 conn.Close();
  181.                 return false;
  182.             }
  183.         }
  184.         //Check whether or not the input information in our loginform matches anything from our database
  185.         public bool IsLogin(string user, string pass)
  186.         {
  187.             string query = $"SELECT * FROM users WHERE username = '{user}' AND password=sha1('{pass}');";
  188.  
  189.  
  190.             try
  191.             {
  192.                 if (OpenConnection())
  193.                 {
  194.                     MySqlCommand cmd = new MySqlCommand(query, conn);
  195.                     MySqlDataReader reader = cmd.ExecuteReader();
  196.  
  197.                     if (reader.Read())
  198.                     {
  199.                         reader.Close();
  200.                         conn.Close();
  201.                         return true;
  202.  
  203.                     }
  204.                     else
  205.                     {
  206.                         reader.Close();
  207.                         conn.Close();
  208.                         return false;
  209.                     }
  210.                 }
  211.                 else
  212.                 {
  213.                     conn.Close();
  214.                     return false;
  215.                 }
  216.             }
  217.             catch (Exception ex)
  218.             {
  219.                 conn.Close();
  220.                 return false;
  221.             }
  222.         }
  223.         //Used to establish connection to our database, which we use to register, login and check if username is taken.
  224.         private bool OpenConnection()
  225.         {
  226.             try
  227.             {
  228.                 if (conn.State != ConnectionState.Open) conn.Open();
  229.                 return true;
  230.             }
  231.             catch (MySqlException ex)
  232.             {
  233.                 switch (ex.Number)
  234.                 {
  235.                     case 0:
  236.                         MessageBox.Show("Connection to server failed!");
  237.                         break;
  238.                     case 1045:
  239.                         MessageBox.Show("Username and password doesnt match!");
  240.                         break;
  241.                 }
  242.                 return false;
  243.             }
  244.         }
  245.         private void textBox1_TextChanged(object sender, EventArgs e)
  246.         {
  247.  
  248.         }
  249.         private void textBox2_TextChanged(object sender, EventArgs e)
  250.         {
  251.  
  252.         }
  253.  
  254.         private void Form1_Load(object sender, EventArgs e)
  255.         {
  256.         }
  257.        
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement