Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using MySql.Data.MySqlClient;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Runtime;
- namespace WindowsFormsApplication6
- {
- public partial class Login : Form
- {
- //Creating a string used to show the name of the logged in user
- public static string currentuser;
- //Creating string used to connect to our database
- private MySqlConnection conn;
- private string server;
- private string database;
- private string uid;
- private string password;
- public Login()
- {
- //Defining our logindata to be able to establish a connection between the program and our database
- server = "localhost";
- database = "mtgworkshop";
- uid = "root";
- password = "";
- //Assigning a string that defines all of our login data
- string connString;
- connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password}";
- //Makes it as a string that performs a new MySqlConnection
- conn = new MySqlConnection(connString);
- InitializeComponent();
- }
- //Button to go to account creation
- private void button2_Click(object sender, EventArgs e)
- {
- panel2.BringToFront();
- panel2.Visible = true;
- }
- //Button to create an account
- private void button3_Click(object sender, EventArgs e)
- {
- //Strings used to check if requirements has been passed
- string user = textBox3.Text;
- string pass = textBox4.Text;
- string firstname = textBox6.Text;
- string lastname = textBox5.Text;
- //Check if password box and re-type password box matches
- if (pass != textBox7.Text)
- {
- MessageBox.Show($"Password doesn't match!");
- }
- //Check if any fields has more than 50 characters, which is not allowed
- else if (user.Length > 50 || pass.Length > 50 || lastname.Length > 50 || firstname.Length > 50 || textBox7.Text.Length > 50)
- {
- MessageBox.Show($"At least one of the fields includes over 50 characters!");
- }
- //Makes sure that username text length is above 4 characters
- else if (user.Length < 4)
- {
- MessageBox.Show($"Username has to be at least 4 characters long!");
- }
- //Makes sure that username text length is above 8 characters
- else if (pass.Length < 8)
- {
- MessageBox.Show($"Password has to be minimal 8 characters long!");
- }
- //Runs a bool to check whether or not the username has been taken
- else if (UserExist(user))
- {
- MessageBox.Show($"Username already exists");
- }
- //If everything else returns true we will create the new accoount
- else
- {
- //Runs our Register code, where we insert our input to the database
- if (Register(user, pass, firstname, lastname))
- {
- MessageBox.Show($"User {user} has been created!");
- textBox3.Text = "";
- textBox4.Text = "";
- textBox5.Text = "";
- textBox6.Text = "";
- textBox7.Text = "";
- panel2.SendToBack();
- panel2.Visible = false;
- }
- //If an error occurs, we will notify the user
- else
- {
- MessageBox.Show($"Error creating {user}. Please try again!");
- }
- }
- }
- //Button to go back to account login
- private void button4_Click(object sender, EventArgs e)
- {
- panel2.SendToBack();
- panel2.Visible = false;
- }
- //Button to login
- private void button1_Click(object sender, EventArgs e)
- {
- //Strings used to check the logininfomation
- string user = textBox1.Text;
- string pass = textBox2.Text;
- //We run our login code to check whether or not the input match anyting in our database
- if (IsLogin(user, pass))
- {
- //We set the current user global variable
- currentuser = user;
- //We redirect to the main menu
- textBox1.Text = "";
- textBox2.Text = "";
- MessageBox.Show($"Welcome back {user}!");
- WindowsFormsApplication6.Mainmenu f1 = new WindowsFormsApplication6.Mainmenu();
- f1.RefToLogin = this;
- this.Visible = false;
- f1.Show();
- }
- else
- {
- MessageBox.Show($"Username and password does not match.");
- }
- }
- //Function used to connect with mySQL database and perform a register formula
- public bool Register(string user, string pass, string firstname, string lastname)
- {
- string query = $"INSERT INTO users (id, username, password, firstname, lastname) VALUES('', '{user}', sha1('{pass}'),'{firstname}','{lastname}')";
- MySqlCommand cmd = new MySqlCommand(query, conn);
- conn.Open();
- cmd.ExecuteNonQuery();
- return true;
- }
- //Function used to check whether or not username is taken
- public bool UserExist(string user)
- {
- string query = $"SELECT * FROM users WHERE username = '{user}';";
- try
- {
- if (OpenConnection())
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- MySqlDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- {
- reader.Close();
- conn.Close();
- return true;
- }
- else
- {
- reader.Close();
- conn.Close();
- return false;
- }
- }
- else
- {
- conn.Close();
- return false;
- }
- }
- catch (Exception ex)
- {
- conn.Close();
- return false;
- }
- }
- //Check whether or not the input information in our loginform matches anything from our database
- public bool IsLogin(string user, string pass)
- {
- string query = $"SELECT * FROM users WHERE username = '{user}' AND password=sha1('{pass}');";
- try
- {
- if (OpenConnection())
- {
- MySqlCommand cmd = new MySqlCommand(query, conn);
- MySqlDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- {
- reader.Close();
- conn.Close();
- return true;
- }
- else
- {
- reader.Close();
- conn.Close();
- return false;
- }
- }
- else
- {
- conn.Close();
- return false;
- }
- }
- catch (Exception ex)
- {
- conn.Close();
- return false;
- }
- }
- //Used to establish connection to our database, which we use to register, login and check if username is taken.
- private bool OpenConnection()
- {
- try
- {
- if (conn.State != ConnectionState.Open) conn.Open();
- return true;
- }
- catch (MySqlException ex)
- {
- switch (ex.Number)
- {
- case 0:
- MessageBox.Show("Connection to server failed!");
- break;
- case 1045:
- MessageBox.Show("Username and password doesnt match!");
- break;
- }
- return false;
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement