So, in my mysql database, I have a whole table for users which contains hashed (with the PASSWORD() mysql function) passwords. In C#, I made a program that can insert new records into the database and display existing ones.
Now I'm at the stage when I want to make a login, which checks if the entered data (username and password) equal to the ones that are in the users table. (The users table contains id, username, password columns.)
What I tried so far:
In my class that is for database operations:
(The c.conn, c.open(), c.close() are just functions defined by me in a class called Connect.)
public bool logIn(string usrName, string pswd)
{
bool success = false;
string dbusrname; string dbpswd;
c.open();
string hashedEnteredPswd = "PASSWORD(@password);";
cmd = new MySqlCommand(hashedEnteredPswd, c.conn);
cmd.Parameters.AddWithValue("@password", pswd);
cmd.ExecuteNonQuery();
c.close();
query = "SELECT * FROM admins;";
c.open();
cmd = new MySqlCommand(query, c.conn);
var reader=cmd.ExecuteReader();
while (reader.Read())
{
dbusrname = reader["users"].ToString();
dbpswd = reader["password"].ToString();
if (dbusrname == usrName && dbpswd == hashedEnteredPswd)
{
success = true;
}
else
{
success = false;
}
}
c.close();
return success;
}
In the code of my form:
private void Container_Load(object sender, EventArgs e)
{
c.open();
clearPanels();
panelLogIn.Visible = true;
}
private void buttonLogIn_Click(object sender, EventArgs e)
{
if (dbops.logIn(textBoxUID.Text, textBoxPSWD.Text))
{
clearPanels();
panelMain.Visible = true;
}
}
In my misery, I tried hashing the entered password with a mysql function in C#(I know, I know...)
Ofc when If I run this, I get the following exception: MySql.Data.MySqlClient.MySqlException: 'PROCEDURE test.PASSWORD does not exist' which is expected and I understand why it doesn't work.
But then, how should do this correctly? How on earth do I make it work?