Click here to Skip to main content
15,749,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have made a login winForm with connection to SQLserver
I set users with level 0 and level 1
SQL
SELECT TOP (1000) [username]
      ,[password]
      ,[niveau]
  FROM [bibliotheque].[dbo].[compte]

user and admin enter the same form but user will have a disbaled button so he can't edit or remove rows

What I have tried:

I have try this for now its only can check if users are in database but doesn't check if they are level 0 or 1
C#
public List<Compte> Connexion(string userName, string password, string niveau)
        {
            var comptes = new List<Compte>();
            using (var connection = factory.CreateConnection())
            {
                connection.ConnectionString = connectionString;
                connection.Open();
                var command = factory.CreateCommand();
                command.Connection = connection;
                string req = "";
                if (userName.Equals("") && password.Equals("")) req = "select * from compte";
                else req = "select * from compte where username='" + userName + "' and password='" + password + "'";
                command.Connection = connection;
                command.CommandText = req;
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Compte c = new Compte();
                        c.UserName = reader["username"].ToString();
                        c.Password = reader["password"].ToString();
                        c.Niveau = Int32.Parse(reader["niveau"].ToString());
                        comptes.Add(c);
                    }
                }

            }
            return comptes;
        }
Posted
Updated 12-Mar-22 2:18am

You have much more serious things to worry about.
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And on a login page? That's just suicidal ...

And

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

Fix both of those throughout your whole app first, and maybe by then you will know enough to fix teh problem you have noticed ...
 
Share this answer
 
Comments
Abderrahmane Radiohead 12-Mar-22 5:52am    
Thank you, mind me I'm just a beginner, I just start learning c# for a while how i can change sql commmand to parameterized queries I'm having difficulties to do that
Luc Pattyn 12-Mar-22 9:09am    
In just 5 minutes you decided you have difficulties doing something you aren't familiar with?
Seems like you didn't bother to study and try at all.
Abderrahmane Radiohead 13-Mar-22 1:58am    
I already know about parametrized queries and about SQL injections and my application is vulnerable before I share this post, I had difficulties to implement it, that's why I only use concatenate strings to build a SQL command on my mini project. But my question wasn't this at all.
Just to let you know I spend hours to fix something on my code before I ask people for help.
OriginalGriff 13-Mar-22 4:16am    
No, but that t is vulnerable means that you are at risk because you don't know what you are doing - and you need to learn that before you move on.

It's like driving a car: if you don't learn where the brakes are and how to use them before you get going then you are going to crash when the traffic lights go red!

Learn the basics, don't try to skip them to get to the "interesting stuff" - they make your whole job easier, not harder!
Abderrahmane Radiohead 13-Mar-22 4:43am    
Okay, thanks for your help sir
In addition to the previous advice from Griff and Maciej, you could add an if in your code like this:
while (reader.Read())
{
    Compte c = new Compte();
    c.UserName = reader["username"].ToString();
    c.Password = reader["password"].ToString();
    c.Niveau = Int32.Parse(reader["niveau"].ToString());

    if (c.Niveau != null && c.Niveau == niveau)
    {
        comptes.Add(c);
    }
}
 
Share this answer
 
C#
req = "select * from compte where username='" + userName + "' and password='" + password + "'";

Your code is Sql injection[^] vulnerable!
You have to change it to avoid data destruction. See:
Data Security: Stop SQL Injection Attacks Before They Stop You | Microsoft Docs[^]

I'd suggest to use parameterized queries instead.
Parameter Queries - Visual Database Tools | Microsoft Docs[^]
Create Parameterized Queries in ADO.NET -- Visual Studio Magazine[^]

If you want to check value in a single column only, use: SqlCommand.ExecuteScalar Method (System.Data.SqlClient) | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900