2016-08-30 206 views
-1

所以基本上管理員和用戶進入不同的窗口,這裏是代碼C#如何在不同的登錄爲管理員和用戶

private void cmdEnter_Click(object sender, EventArgs e) 
     { 
      if (txtUsername.Text == "" && txtPassword.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Username and Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else if (txtUsername.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Username", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else if (txtPassword.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

      else 
      { 
       try 
       { 
        string myConnection = "datasource=localhost;port=3306;username=root"; 
        MySqlConnection myConn = new MySqlConnection(myConnection); 

        MySqlCommand SelectCommand = new MySqlCommand("select * from boardinghousedb.employee_table where username='" + this.txtUsername.Text + "' and password='" + this.txtPassword.Text + "' ;", myConn); 

        MySqlDataReader myReader; 

        myConn.Open(); 
        myReader = SelectCommand.ExecuteReader(); 
        int count = 0; 
        while (myReader.Read()) 
        { 
         count = count + 1; 
        } 
        if (count == 1) 
        { 
         MessageBox.Show("Username and Password . . . is Correct", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
         this.Hide(); 
         Menu mm = new Menu(); 
         mm.ShowDialog(); 
        } 
        else if (count > 1) 
        { 
         MessageBox.Show("Duplicate Username and Password . . . Access Denied", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        } 
        else 
        { 
         MessageBox.Show("Username and Password is Not Correct . . . Please try again", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         myConn.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      }  
     } 

,但我不知道我怎麼了,其他教程關於本地數據庫會談,但很使用MySQL Here is the employee table, title=admin or user

+2

對於新手來說,這個代碼是SQL注入敞開的。您將要查看參數化查詢。除此之外,我不清楚你在問什麼。這段代碼不工作的方式是什麼?有什麼問題? – David

+1

似乎我可以登錄*您的任何*帳戶,只要我的密碼是'PLAIN WRONG'UNION SELECT * from boardinghousedb.employee_table LIMIT 1; - ' – nvoigt

+0

您檢查'if count == 1'是否正確..如果您正在檢查重複項,這仍然是不正確的..您需要構建您的數據庫,以便您擁有用戶和管理員以及結構的Id你的查詢返回那個..然後在你的查詢中,如果用戶名和密碼是正確的,那麼檢查他們是否嘗試訪問管理員,當他們應該是一個用戶時,然後顯示一條消息並學習如何在檢查後使用'return'關鍵字消息..如果你有空白的用戶和密碼,你需要顯示消息,並立即'返回'意味着退出該方法.. – MethodMan

回答

1

您的代碼有幾個問題。

  1. 你應該在你的數據庫中創建唯一約束,以避免用戶重複的用戶名
  2. 你應該讓你的密碼散列,而不是純文本。這樣,如果有人進入您的數據庫,他仍然無法讀取密碼。
  3. 您應該使用SQL參數化查詢來避免SQL注入。 您的查詢很容易發生SQL注入。 SQL注入是一種將SQL命令注入到查詢中的方法。某些用戶可能會在您的用戶名文本框中輸入someName' OR 1=1--,並且您的查詢會翻譯爲select * from boardinghousedb.employee_table where username='someName' OR 1=1--。最後注意--,這會使查詢的其餘部分被註釋掉。您可以在this鏈接閱讀更多內容。如果你被允許,我建議你看看EntityFramework。這是查詢數據庫的強大工具。
  4. 使用finally塊捕獲後關閉你的數據庫連接。

與你的問題相關,如果你想區分管理員和用戶,你需要引入某種角色或至少bool值,你爲那個用戶指定IsAdmin

然後,您可以根據自己的需要將代碼放置到單獨的函數/函數/類中,並使用WHERE Role='Admin'或類似查詢來查詢用戶。

舉一個例子

public bool IsValidLogin(string username, string password); 

public bool IsValieLoginForAdmin(string username, string password); 

或者其他任何你喜歡的實現。

,然後在下面的方式重新使用它:

private void cmdEnter_Click(object sender, EventArgs e) 
{ 
    if(IsValidLogin("username", "password")) 

    //or 

    if(IsValidLoginForAdmin("username", "password")) 

//do something 

} 

編輯:

你也可以引入新的列到你的餐桌,caled UserRole。爲了簡單起見,我只是修改你的代碼,你可以在學習時重新考慮它。

MySqlCommand SelectCommand = new MySqlCommand("select * from boardinghousedb.employee_table where username='" + this.txtUsername.Text + "' and password='" + this.txtPassword.Text + "' ;", myConn); 

        MySqlDataReader myReader; 

        myConn.Open(); 
        myReader = SelectCommand.ExecuteReader(); 
        int count = 0; 
        string userRole = string.Empty; 
        while (myReader.Read()) 
        { 
         count = count + 1; 
         userRole = myReader["UserRole"].ToString(); 
        } 
        if (count == 1) 
        { 
         MessageBox.Show("Username and Password . . . is Correct", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
         this.Hide(); 
         if(userRole =="Admin") 
         //show admin window 
         else 
         //show user window 
         Menu mm = new Menu(); 
         mm.ShowDialog(); 
        } 
+0

我仍然是一名學生,我不知道我們的學校是否落後,但說實話,你所說的對我來說都是新的。 。我甚至不知道SQL注入是什麼意思:( –

+0

@LeeJun我已經在我的回答 – Robert

+0

@LeeJun中添加了更多關於sql注入的信息,請檢查我的編輯解決方案。 – Robert

-2

代碼

  if (txtUsername.Text == "" && txtPassword.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Username and Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else if (txtUsername.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Username", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
      else if (txtPassword.Text == "") //Error when all text box are not fill 
      { 
       MessageBox.Show("Unable to fill Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 

      else 
      { 
       try 
       { 
        string myConnection = "datasource=localhost;port=3306;username=root"; 
        MySqlConnection myConn = new MySqlConnection(myConnection); 

        MySqlCommand SelectCommand = new MySqlCommand("select * from boardinghousedb.employee_table where username='" + this.txtUsername.Text + "' and password='" + this.txtPassword.Text + "' ;", myConn); 

        MySqlDataReader myReader; 

        myConn.Open(); 
        myReader = SelectCommand.ExecuteReader(); 
        int count = 0; 
        while (myReader.Read()) 
        { 
         count = count + 1; 
        } 
        if (count == 1) 
        { 
         MessageBox.Show("Username and Password . . . is Correct", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
         this.Hide(); 
         Menu mm = new Menu(); 
         mm.ShowDialog(); 
        } 
        else if (count > 1) 
        { 
         MessageBox.Show("Duplicate Username and Password . . . Access Denied", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        } 
        else 
        { 
         MessageBox.Show("Username and Password is Not Correct . . . Please try again", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
         myConn.Close(); 
        } 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 
      }  

此代碼缺少它的返回類型的錯誤說