Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
This is my signup code, i have used md5 to hash the password. what is the code of sign-in button?
C#
private void button5_Click(object sender, EventArgs e)
       {
           SqlConnection myconnection;
           SqlCommand mycommand;
           int ra;
           string query;
            Byte[] hashedDataBytes;
           myconnection = new SqlConnection("Data Source=AMEERA-PC\\SQLEXPRESS;Initial Catalog=dbbaas;User ID=baas1;Password=baas123");
           myconnection.Open();
           MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();

           UTF8Encoding encoder = new UTF8Encoding();
           hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(textBox3.Text));
           StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
           foreach (Byte b in hashedDataBytes)
           {
               hex.AppendFormat("{0:x2}", b);
           }
           label3.Text = hex.ToString();


           query = "Insert into nuser ( uid,uname,enpwd) values(" + textBox1.Text + ",'" +
           textBox2.Text + "','" + hex.ToString() + "')";
           mycommand = new SqlCommand(query, myconnection);
           ra = mycommand.ExecuteNonQuery();

           if (ra > 0)
           {
               MessageBox.Show("Record Inserted Sucessfuly");
           }
           else
           {
               MessageBox.Show("Unable to Insert Record ");
           }
           myconnection.Close();


       }

please help , i need the code today?
thanks in advance
Posted
Updated 27-Dec-11 18:02pm
v2
Comments
Amir Mahfoozi 28-Dec-11 0:09am    
read my answer here : http://www.codeproject.com/Answers/280703/store-procedure-example#answer4

Hi, At the time of login, first encrypt entered password in textbox and compare this with database and check authenticate or not.
 
Share this answer
 
Hi, you need to again write following code in login button

C#
hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(textBox3.Text));
            StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
            foreach (Byte b in hashedDataBytes)
            {
                hex.AppendFormat("{0:x2}", b);
            }
            label3.Text = hex.ToString();
 

            query = "Select * from nuser  where uname='" + textBox1.Text + "' and enpwd ='" + hex.ToString() + "')";
            mycommand = new SqlCommand(query, myconnection);
            ra = mycommand.ExecuteNonQuery();
 
Share this answer
 
Hello friend,

when you are retrieving data (password) back from database then decrypt that and compare. And i did this using UTF8 and decoded back :

like this :
C#
public string DecryptString(string pwd)
        {
          try
          {
            UTF8Encoding encoder = new System.Text.UTF8Encoding();
            Decoder utf8Decode = encoder.GetDecoder();

            byte[] toDecode_byte = Convert.FromBase64String(pwd);
            int charCount = utf8Decode.GetCharCount(toDecode_byte, 0, toDecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(toDecode_byte, 0, toDecode_byte.Length, decoded_char, 0);
            string decodedPwd = new string(decoded_char);
            return decodedPwd;
          }
          catch
          {
            return null;
          }
        }

Hope this will help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
XML
while sign in you need to fetch the password based on the user entered user name.
<pre lang="c#">
Select * from nuser where uname='textbox1.text';
</pre>

<pre lang="c#">
        MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();

        UTF8Encoding encoder = new UTF8Encoding();
        hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(Password.Text));
        StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
        foreach (Byte b in hashedDataBytes)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        //label3.Text = hex.ToString();

        if ( FromDatabasepwd == hex.ToString())
        {
            // correct password
        }
        else
        {
            // incorrect pwd
        }
</pre>
 
Share this answer
 
WHAT DO YOU MEAN BY THIS FromDatabasepwd?IT GENERATE ERROR[DOES NOT EXIST IN CURRENT COTEXT]
C#
if ( FromDatabasepwd == hex.ToString())
       {
           // correct password
       }
 
Share this answer
 

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