2012-04-02 22 views
2

ユーザーの提供情報がデータベースにあり、正しい条件を入力しようとしましたが、それが機能していないと検証する必要があります。データベース。私のコードをチェックして、何が起こっているか教えてください。 、私はあなたが必要とするすべてはあなたがtrueにisExistを設定するときに、あなたのforeachループから抜け出すことだと思い、私はそれをデバッグしようとしたが、foreachループは、ループを継続している場合(isexist)の文条件が真であればコードは返されません

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     token = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUsername.Text.ToString() + txtAcctNo.Text.ToString(), "MD5"); 
     try 
     { 
      bool isExist = false; 
      DataSet ds = new DataSet(); 
      ds = startService.getAllUsersWithoutFilter(); 
      if (ds.Tables[0].Rows.Count > 0) 
      { 
       foreach (DataRow dRow in ds.Tables[0].Rows) 
       { 

        string userName = dRow["UserName"].ToString(); 
        string acctNo = dRow["AccountNumber"].ToString(); 
        string question = dRow["SecretQuestion"].ToString(); 
        string answer = dRow["SecretAnswer"].ToString(); 

        if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() && answer == txtAnswer.Text.ToString()) 
        { 
         isExist = true; 
        } 
        else 
        { 
         isExist = false; 
        } 

       } 

       if (isExist) 
       { 
        startService.sendTokenizer(txtUsername.Text.ToString(), token); 
        //update database to change password to standard password 
        startService.inserUserActivity(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), "Password Reset Request", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]); 
        startService.requestReset(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), token); 

        lblMessage.ForeColor = System.Drawing.Color.Green; 
        lblMessage.Text = "<br>We have sent an email to you for the instructions to reset your password. Please check your email."; 
       } 
       else 
       { 
        this.lblMessage.ForeColor = System.Drawing.Color.Red; 
        this.lblMessage.Text = "<br><br>Error - Information cannot be found. Please check and try again. Make sure all the fields are correct."; 
       } 

      } 
     } 
     catch 
     { 
      lblError.Text = "There was an error occured while processing your request. Please try again later."; 
     } 

    } 

答えて

5

に行っていません。

if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() && answer == txtAnswer.Text.ToString()) 
{ 
    isExist = true; 
    break; //Found it, so stop looking. 
} 
+0

おかげで...それ+1 – Dhenn

2

あなたの質問に対する直接的な回答について、私はジョエルが正しいと思います。

ユーザーテーブル全体を読み込んでそれをWebサーバー上で反復処理することを再考する必要があります。なぜデータベースから一致する行を選択しようとしないのですか?一致した場合、資格情報は有効です。そうでない場合、それらは有効ではなかった。

+0

、今[OK]をクリックして、コードを次のように変更を加える必要がある - 確かに良いアイデア、テーブル全体をロードして、データベースの力を利用しないために検証のために一致する行を見つける。 – TLS

0

@Dhenn:あなたは

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    token = FormsAuthentication.HashPasswordForStoringInConfigFile(txtUsername.Text.ToString() + txtAcctNo.Text.ToString(), "MD5"); 
    try 
    { 
     bool isExist = false; 
     DataSet ds = new DataSet(); 
     ds = startService.getAllUsersWithoutFilter(); 
     if (ds.Tables[0].Rows.Count > 0) 
     { 
      foreach (DataRow dRow in ds.Tables[0].Rows) 
      { 

       string userName = dRow["UserName"].ToString(); 
       string acctNo = dRow["AccountNumber"].ToString(); 
       string question = dRow["SecretQuestion"].ToString(); 
       string answer = dRow["SecretAnswer"].ToString(); 

       if (userName == txtUsername.Text.ToString() && acctNo == txtAcctNo.Text.ToString() && question == cboQuestion.Text.ToString() && answer == txtAnswer.Text.ToString()) 
       { 
        // if exist execute following code 

        startService.sendTokenizer(txtUsername.Text.ToString(), token); 
       //update database to change password to standard password 
       startService.inserUserActivity(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), "Password Reset Request", HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]); 
       startService.requestReset(txtUsername.Text.ToString(), txtAcctNo.Text.ToString(), token); 

       lblMessage.ForeColor = System.Drawing.Color.Green; 
       lblMessage.Text = "<br>We have sent an email to you for the instructions to reset your password. Please check your email."; 
       } 
       else 
       { 
        // id not exist then execute following code 

        this.lblMessage.ForeColor = System.Drawing.Color.Red; 
       this.lblMessage.Text = "<br><br>Error - Information cannot be found. Please check and try again. Make sure all the fields are correct."; 
       } 

      }    

     } 
    } 
    catch 
    { 
     lblError.Text = "There was an error occured while processing your request. Please try again later."; 
    } 

}