2014-09-25 49 views
1

我的登錄表單正在運行,但是當我在單詞的第一個字母中輸入大寫字母的用戶名並且數據庫中的用戶名全部爲小寫字母時,仍然允許訪問和登錄成功的。登錄表單中的大小寫敏感C#

你能幫我解決這個問題嗎? 我真的需要你的幫助。

這是我當前的代碼

private void button1_Click(object sender, EventArgs e) 
      { 





       if (textBox1.Text == "" || textBox2.Text == "") 
       { 
        MessageBox.Show("Input Required Fields!", 
      "Note", 
      MessageBoxButtons.OK, 
      MessageBoxIcon.Exclamation, 
      MessageBoxDefaultButton.Button1); 
       } 

       else 
       { 

        //Passing the value from textbox 
        Tic_Tac_Toe frm2 = new Tic_Tac_Toe(textBox1.Text); 
        View view = new View(textBox1.Text); 

        string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Majel\Tic Tac Toe\Database\Database.mdb"; 

        OleDbConnection con = new OleDbConnection(str); 
        OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM data WHERE Users = '" + textBox1.Text + "' AND Pass = '" + textBox2.Text + "'", con); 

        con.Open(); try 
        { 
         int i; 

         i = Convert.ToInt32(cmd.ExecuteScalar()); 

         if (i == 1) 
         { 
          MessageBox.Show("Login Successful!", 
this.Hide(); 
         frm2.Show(); 


        } 


        else 
        { 
         MessageBox.Show("Invalid User Name or Password", 
     "Note", 
     MessageBoxButtons.OK, 
     MessageBoxIcon.Exclamation, 
     MessageBoxDefaultButton.Button1); 

        } 

       } 

       catch (Exception ex) 
       { 

        MessageBox.Show(ex.Message); 

       } 

       finally 
       { 

        con.Close(); 

       } 
      } 
     } 

有人能幫助我嗎?提前謝謝你。

+1

添加'WHERE'條款到SQL制約了登錄用戶的變更到你。 – DeanOC 2014-09-25 23:50:30

回答

1

很簡單,你需要一個WHERE條款。

string SqlString =「更新數據SET Player1 =?,Player2 =?WHERE UserName = @ user」;

沒有它,更新將應用於表中的所有行。請注意,這與DELETE語句完全相同。

現在,您通常不會像這樣的用戶名匹配,您可以使用表主鍵進行這些類型的比較。除此之外,這是SQL 101.我強烈建議在嘗試繼續更進一步之前獲取RDBMS書籍並學習SQL的基礎知識。

1

我可以說,你保存在數據庫密碼,必須保存散列Algorithm密碼數據,如MD5SHA1
當登錄用戶輸入密碼,你湊那個鍵入的密碼字符串並將此字符串密碼保存在DB

public static void HashPassword(string Password, out string Salt, out string Hash) 
    { 
     System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed(); 
     Random rnd = new Random(); 
     byte[] s = new byte[20]; 
     rnd.NextBytes(s); 
     Salt = Convert.ToBase64String(s); 
     System.Text.UTF8Encoding u = new UTF8Encoding(); 
     byte[] pass = u.GetBytes(Password); 
     byte[] all = new byte[pass.Length + s.Length]; 
     Array.Copy(pass, all, pass.Length); 
     Array.Copy(s, 0, all, pass.Length, s.Length); 
     Byte[] H = sha.ComputeHash(all); 
     Hash = Convert.ToBase64String(H); 
    } 

    public bool IsPasswordCorrect(string Password, string Salt, string Hash) 
    { 
     System.Security.Cryptography.SHA1Managed sha = new System.Security.Cryptography.SHA1Managed(); 
     byte[] s = Convert.FromBase64String(Salt); 
     System.Text.UTF8Encoding u = new UTF8Encoding(); 
     byte[] pass = u.GetBytes(Password); 
     byte[] all = new byte[pass.Length + s.Length]; 
     Array.Copy(pass, all, pass.Length); 
     Array.Copy(s, 0, all, pass.Length, s.Length); 
     Byte[] H = sha.ComputeHash(all); 
     return (Hash == Convert.ToBase64String(H)); 
    } 

現在你必須使用HashPassword方法給哈希的鹽和保存散列和鹽,以分貝爲每一位用戶。
當要檢查密碼使用IsPasswordcorrect方法

爲您的代碼

private void button1_Click(object sender, EventArgs e) 
      { 
       if (textBox1.Text == "" || textBox2.Text == "") 
       { 
        MessageBox.Show("Input Required Fields!", 
      "Note", 
      MessageBoxButtons.OK, 
      MessageBoxIcon.Exclamation, 
      MessageBoxDefaultButton.Button1); 
       } 

       else 
       { 

        //Passing the value from textbox 
        Tic_Tac_Toe frm2 = new Tic_Tac_Toe(textBox1.Text); 
        View view = new View(textBox1.Text); 

        string str = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Majel\Tic Tac Toe\Database\Database.mdb"; 

        OleDbConnection con = new OleDbConnection(str); 
        OleDbCommand cmd = new OleDbCommand("SELECT * FROM data WHERE Users = '" + textBox1.Text, con); 
        OleDbDataAdapter ta=new OleDbDataAdapter(cmd); 
        DataSet ds=new DataSet(); 
        try 
        { 
        ta.Fill(ds); 
        if(ds.Tables[0].Rows.Count==0) 
        { 
         MessageBox.Show("User Dos not Exists"); 
         Application.Exit(); 
        } 
        string hash=ds.Tables[0].Rows[0]["password"].ToString(); 
        string salt=ds.Tables[0].Rows[0]["salt"].ToString(); 
         if(IsPasswordCorrect(textBox2.Text,salt,hash)) 
         { 
          MessageBox.Show("Success"); 

         this.Hide(); 
         frm2.Show(); 


        } 


        else 
        { 
         MessageBox.Show("Invalid User Name or Password", 
     "Note", 
     MessageBoxButtons.OK, 
     MessageBoxIcon.Exclamation, 
     MessageBoxDefaultButton.Button1); 

        } 

       } 

       catch (Exception ex) 
       { 

        MessageBox.Show(ex.Message); 

       } 

       finally 
       { 

        con.Close(); 

       } 
      } 
     } 
+0

你能提供一個例子嗎? – user3349418 2014-09-28 07:46:55

+0

我改變了答案 – 2014-09-28 08:04:40

+0

@ Mehdi Haghshena,如果你會介意先生,我只想問一下,如何在我的代碼中使用它?你能提供嗎?先生?實際上,這對我來說太複雜了,我只是一個c#中的新手。 – user3349418 2014-09-28 08:15:32