1

VS2010 を使用して Windows フォーム アプリケーションを構築しています。ユーザーIDとパスワードを読み取り、データベースでそれらの有効性を確認したい。適切な一致が見つかった場合は、ユーザーをメイン フォームに誘導します。適切な資格情報を入力しても、コードが現在指示していません。原因は何ですか?

string connectionstring = "Data Source=localhost;Initial Catalog=HMS;Persist Security Info=True;User ID=Developer;Password=abc@123";
SqlConnection connection = new SqlConnection(connectionstring);

string SelectStatement = "SELECT * FROM Users where UserID = '@UserID' and Password = '@password'";
SqlCommand insertcommand = new SqlCommand(SelectStatement, connection);
insertcommand.Parameters.AddWithValue("@UserID", textBox1.Text);
insertcommand.Parameters.AddWithValue("@password", textBox2.Text);
SqlDataReader reader;

try
{
  connection.Open();
  reader = insertcommand.ExecuteReader();

  while (reader.Read())
  {
    string userid = reader["UserID"].ToString();
    string pass = reader["Password"].ToString();
    if (userid == textBox1.Text.ToString() && pass == textBox2.Text.ToString()) // login successful
    {
      Mainform main = new Mainform();
      this.Hide();
      main.Show();
    }
    else
    {
      MessageBox.Show("Invalid Username or Password!");
    }
  }//end while
  reader.Close();
  }
  catch (Exception ex)
  {
  throw ex;

}//end catch
finally
{
  connection.Close();
}
4

1 に答える 1

0

まず、パスワードを生のテキストとしてデータベースに保存しないでください。それはただトラブルを求めているだけです。ソルトハッシュをデータベースに保存し、クライアントでハッシュを計算して比較する必要があります。

次に、main.ShowDialog() を試して、メイン フォームを維持します。

于 2012-06-14T16:25:53.427 に答える