1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| using MetroFramework.Forms;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RPGM_Share_Manager
{
public partial class Form1 : MetroForm
{
private int ID;
private string IP;
private string username;
private string email;
private string date_inscrit;
public Form1()
{
InitializeComponent();
this.txtUsername.Text = (string)Properties.Settings.Default.Identifiant;
this.txtPassword.Text = (string)Properties.Settings.Default.Password;
if (this.txtUsername.Text != "" && this.txtPassword.Text != "")
this.metroToggle1.Checked = true;
}
private void SaveIdentifiant()
{
if (this.metroToggle1.Checked == true)
{
Properties.Settings.Default.Identifiant = (string)this.txtUsername.Text;
Properties.Settings.Default.Password = (string)this.txtPassword.Text;
}
else
{
Properties.Settings.Default.Identifiant = "";
Properties.Settings.Default.Password = "";
}
Properties.Settings.Default.Save();
}
private void metroButton1_Click(object sender, EventArgs e)
{
SaveIdentifiant();
ConnectDataSQL();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Inscription i = new Inscription();
i.ShowDialog();
}
static string GetMd5Hash(MD5 md5Hash, string input)
{
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
// Verify a hash against a string.
static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
{
// Hash the input.
string hashOfInput = GetMd5Hash(md5Hash, input);
// Create a StringComparer an compare the hashes.
StringComparer comparer = StringComparer.OrdinalIgnoreCase;
if (0 == comparer.Compare(hashOfInput, hash))
{
return true;
}
else
{
return false;
}
}
public int valueID()
{ return this.ID; }
string hash;
private void ConnectDataSQL()
{
using (MD5 md5Hash = MD5.Create())
{
hash = GetMd5Hash(md5Hash, this.txtPassword.Text);
Console.WriteLine("The MD5 hash of " + this.txtPassword.Text + " is: " + hash + ".");
Console.WriteLine("Verifying the hash...");
if (VerifyMd5Hash(md5Hash, this.txtPassword.Text, hash))
{
Console.WriteLine("The hashes are the same.");
}
else
{
Console.WriteLine("The hashes are not same.");
}
}
var userHost = Dns.GetHostName();
var GetComputer_InternetIP = Dns.GetHostEntry(userHost).AddressList[2].ToString();
MySqlConnection conn = new MySqlConnection("SERVER = db4free.net; PORT = 3306; DATABASE = drocs; USERID = ; PASSWORD = ");
conn.Open();
MySqlCommand ip_user = new MySqlCommand("UPDATE User SET ip ='" + GetComputer_InternetIP.ToString() + "' WHERE username='" + this.txtUsername.Text + "'", conn);
MySqlCommand verify_user = new MySqlCommand("SELECT userid, username, password, email, date, ip FROM User WHERE username='" + this.txtUsername.Text + "' and password = '" + hash + "'", conn);
MySqlCommand add_friend = new MySqlCommand("SELECT ID_Personne_Principal, ID_Personne_Ami FROM List_Amis WHERE ID_Personne_Principal = '" + ID + "'", conn);
try
{
MySqlDataReader reader_verify_user = verify_user.ExecuteReader();
if (reader_verify_user.HasRows)
{
while (reader_verify_user.Read())
{
ID = reader_verify_user.GetInt32(0);
username = reader_verify_user.GetString(1);
email = reader_verify_user.GetString(3);
date_inscrit = reader_verify_user.GetString(4);
IP = reader_verify_user.GetString(5);
}
reader_verify_user.Close();
conn.Close();
conn.Open();
ip_user.ExecuteNonQuery();
conn.Close();
conn.Open();
MySqlCommand verify_user_connexion = new MySqlCommand("SELECT userid FROM Connexion WHERE userid = '" + ID + "'", conn);
MySqlDataReader reader_user_connexion = verify_user_connexion.ExecuteReader();
if (reader_user_connexion.HasRows)
{
reader_user_connexion.Close();
if (MessageBox.Show("Vous êtes déjà connecter!\nSe reconnecter?", "Server", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
MySqlCommand delete_connexion = new MySqlCommand("DELETE FROM Connexion WHERE userid = '" + ID + "'", conn);
try
{
delete_connexion.ExecuteNonQuery();
conn.Close();
}
catch (Exception)
{
throw;
}
}
}
else
{
reader_user_connexion.Close();
conn.Close();
conn.Open();
MySqlCommand insert_connexion = new MySqlCommand("INSERT INTO Connexion(IP, userid, last_connexion) VALUE('" + GetComputer_InternetIP.ToString() + "', '" + ID + "', '" + DateTime.Now.ToString() + "')", conn);
insert_connexion.ExecuteNonQuery();
conn.Close();
this.Hide();
Accueil a = new Accueil(ID, username, email);
a.Show();
}
}
else
{
MessageBox.Show("L'identifiant ou le mot de passe est incorrect!", "Connexion", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
conn.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
throw;
}
}
}
} |
Partager