Search code examples
mysqlasp.netforms-authenticationsha256salt-cryptography

How to get salt from MySql database using Asp.net?


How can I retrieve salt from MySql database using Asp.Net ?

I want to use that retrieved salt to add to the user entered password to generate an SHA256 hash and then authenticate the user.

Here is what I am trying to do to fetch the salt:

String userNameEntered = UserN_TextBox.Text;
String passwordEntered = Password_TextBox.Text;
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
connection = new MySqlConnection(connectionString);
connection.Open();
MessageBox.Show("Successfully connected to database");
String queryString = "select salt from xyz.abc_table where salt = @Salt";
command = new MySqlCommand(queryString, connection);
command.Parameters.AddWithValue("@Salt", queryString);
reader = command.ExecuteReader();
Response.Write("Salt retrived is" + reader);
reader.Close();
connection.Close();

When I execute this code, it returns the MySql Data Reader library rather than the salt in the database....

Thanks in advance... :)


Solution

  • Here I have solved my problem. Here is the solution to the problem. It might help someone:

    try
            {
    
                String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                connection = new MySqlConnection(connectionString);
                connection.Open();
                MessageBox.Show("Successfully connected to database");
                String queryString = "select salt from xyz.abc_table where email_address = @E_Address";
                command = new MySqlCommand(queryString, connection);
                command.Parameters.AddWithValue("@E_Address", UserN_TextBox.Text);
                reader = command.ExecuteReader();
                if (reader.Read())
                {
                    Response.Write("Retrived Salt is " + reader["salt"]);
                    reader.Close();
                    connection.Close();
                }
    
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed due to" +ex);
            }