Bonjour, j'ai un énorme soucis, j'ai fais mon logiciel puis je me suis rendu compte que.. il était vulnérable aux injections SQL donc j'ai voulu mettre en place des requêtes paramétrées, alors quand je les ai testées sur le panel MySql la syntaxe était correcte mais quand je le fais via le logiciel absolument pas, j'ai cherché sur divers sites anglais/français mais rien n'a résolu mon problème, j'ai donc tout remis comme avant c'est à dire très vulnérable et j'aimerai savoir comment le convertir en requêtes paramétrées sans erreurs :/

Je tiens à préciser que c'est la première fois que j'utilise du MySql sur C#

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
            string query = "SELECT * FROM staff WHERE `mdp` = '" + textBox4.Text + "'";
            cnx.Open();
            if (textBox4.Text == "" || textBox4.Text == " ")
            {
 
            }
            MySqlDataAdapter datardr = new MySqlDataAdapter(query, cnx);
            DataTable dttable = new DataTable();
            datardr.Fill(dttable);
            if (dttable.Rows.Count == 1)
            {
                string query2 = "SELECT nom FROM staff WHERE `mdp` = '" + textBox4.Text + "'";
                MySqlCommand commande2 = new MySqlCommand(query2, cnx);
                string nomstaff = (string)commande2.ExecuteScalar();
                MessageBox.Show("Bienvenue "+ nomstaff+", les options réservées au membres du Staff sont désormais activées !", "Mode Staff Activé", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                nsComboBox1.Items.Add("Administration");
                nsComboBox1.Items.Add("Évènements");
                nsComboBox1.Items.Add("Sanctions du Serveur");
                textBox4.Enabled = false;
                nsButton2.Text = "Clé Activée !";
                button2.Enabled = true;
                button2.Text = "Surveillance Joueurs";
                button1.Enabled = true;
                button1.Text = "Membres du Staff";
                nsButton2.Enabled = false;
                if(nomstaff == "masqué" || nomstaff == "masqué" || nomstaff == "masqué"  || nomstaff == "masqué" || nomstaff == "masqué")
                {
                    Class1.getData = "Autorisé";
                    Class1.permissionsstaff = "Autorisé";
                }
 
 
            }
            else
            {
                MessageBox.Show("Erreur : Cette clé n'existe pas, merci d'inscrire une clé valide.", "Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            cnx.Close();
Et voici le code "sécurisé" avec l'erreur(Erreur ligne 21) : Erreur dans la syntaxe

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
string ConnexionSQL = "SERVER=sql11.freemysqlhosting.net; PORT=3306; DATABASE=sql11229992; UID=sql11229992;PWD=uTbtVndAeI;";
            maConnexionSQLsrv = new MySqlConnection(ConnexionSQL);
            //string query = "SELECT * FROM staff WHERE `mdp` = '" + textBox4.Text + "'";
            maRequete = "SELECT * FROM staff WHERE `mdp` = @mdpjoueur";
            myCommand = new MySqlCommand(maRequete, maConnexionSQLsrv);
            myCommand.Parameters.Add("@mdpjoueur", textBox4.Text);
            // myCommand.Parameters["@mdpjoueur"].Value = textBox4.Text;
            MessageBox.Show("OK");
 
            try
            {
                //Execution de la requête 
                MySqlDataAdapter datardr = new MySqlDataAdapter(System.Convert.ToString(myCommand), maConnexionSQLsrv);
                DataTable dttable = new DataTable();
                datardr.Fill(dttable);
                //"INSERT INTO [article] ( [idclient],[idarticle],[prixarticle]) VALUES(@id_article, @prix_article, @nom_article)"; 
 
                if (dttable.Rows.Count == 1)
                {
                    c = new MySqlCommand("select nom from staff where mdp = @Value;", maConnexionSQLsrv);   //<-------- l'érreur est ici
                    c.Parameters.AddWithValue("@Value", textBox4.Text);
 
 
                  //  maRequete2 = "SELECT `nom` FROM staff WHERE `mdp` like(@mdpjoueur)";
                   // myCommand2 = new MySqlCommand(maRequete2, maConnexionSQLsrv);
                   // myCommand2.Parameters["@mdpjoueur"].Value = textBox4.Text;
                    try
                    {
                        //Execution de la requête 
                        maConnexionSQLsrv.Open();
                        string nomstaff = (string)c.ExecuteScalar();
                        nomstaffmsgbox = nomstaff;
                        MessageBox.Show(nomstaff);
                        maConnexionSQLsrv.Close();
 
                    }
                    catch (MySqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
 
                    //  string query2 = "SELECT nom FROM staff WHERE `mdp` = '" + textBox4.Text + "'";
 
                    MessageBox.Show("Bienvenue " + nomstaffmsgbox + ", les options réservées au membres du Staff sont désormais activées !", "Mode Staff Activé", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    nsComboBox1.Items.Add("Administration");
                    nsComboBox1.Items.Add("Évènements");
                    nsComboBox1.Items.Add("Sanctions du Serveur");
                    textBox4.Enabled = false;
                    nsButton2.Text = "Clé Activée !";
                    button2.Enabled = true;
                    button2.Text = "Surveillance Joueurs";
                    button1.Enabled = true;
                    button1.Text = "Membres du Staff";
                    nsButton2.Enabled = false;
                    if (nomstaff == "Havanna" || nomstaff == "privé" || nomstaff == "privé" || nomstaff == "privé" || nomstaff == "privé")
                    {
                        Class1.getData = "Autorisé";
                        Class1.permissionsstaff = "Autorisé";
                    }
 
 
                }
                else
                {
                    MessageBox.Show("Erreur : Cette clé n'existe pas, merci d'inscrire une clé valide.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
J'espère que vous pourrez m'aider :/