2016-08-30 167 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); 
       } 
      }  

此代码缺少它的返回类型的错误说

相关问题