2012-12-17 42 views
0

我使用文本框創建了一個基本的登錄頁面。我正在驗證創建的數據庫中的用戶。下面是我的後端代碼:登錄頁面沒有正確重定向

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    SqlDataReader sdrDatanew = null; //Read rows one by one. 
    string strnew; 
    string connectionString = WebConfigurationManager.ConnectionStrings["Gen_LicConnectionString"].ConnectionString; //Define new connectionstring 
    SqlConnection connew = new SqlConnection(connectionString); //establishes a new sql connection 
    connew.Open(); //Opens Connection 
    strnew = "select User_Type from User_Details where User_Type='" + ddlUserSel.SelectedItem.Value + "' AND LoginID = '" + txtUserName.Text + "' AND Password = '" + txtPassword.Text + "'"; 
    SqlCommand sqlCommnew = new SqlCommand(strnew, connew); //passes the command and connection 
    sdrDatanew = sqlCommnew.ExecuteReader(); //For select command 

    int userType = 0; 

    if (sdrDatanew.HasRows) 
    { 
     if (sdrDatanew.Read()) 
     { 
      userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString()); 
     } 
    } 

    switch (userType) 
    { 
     case 0: 
      Response.Redirect("Lic_Gen.aspx"); 
      break; 
     case 1: 
      Response.Redirect("Cust_Page.aspx"); 
      break; 
     default: 
      lblDisp.Text= "Invalid User/Password"; 
      break; 
    } 

    connew.Close(); 
} 

這是我的前端代碼:

<div> 
    <h2 class="style12"> 
     LOGIN</h2> 
    <p class="style10"> 
     &nbsp;</p> 
    <table align="center" cellpadding="2" cellspacing="5" style="border-width: thick; 
     border-style: outset; height: 195px;" class="style14"> 
     <tr> 
      <td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif; 
       font-size: 17.5px; font-weight: bold; font-style: normal"> 
       User Type</td> 
      <td class="style17"> 
       <asp:DropDownList ID="ddlUserSel" runat="server" Height="25px" Width="260px" CssClass="drp"> 
        <asp:ListItem Value="0">Admin</asp:ListItem> 
        <asp:ListItem Value="1">Customer</asp:ListItem> 
       </asp:DropDownList> 
      </td> 
     </tr> 
     <tr> 
      <td class="style16" style="border-style: none; font-family: 'Times New Roman', Times, serif; 
       font-size: 17.5px; font-weight: bold; font-style: normal"> 
       Login 
      </td> 
      <td class="style17"> 
       <asp:TextBox ID="txtUserName" runat="server" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox> 
       <asp:RequiredFieldValidator ID="reqUserName" runat="server" ErrorMessage="*" ForeColor="Red" 
        ControlToValidate="txtUserName" Font-Size="Large"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td class="style16" style="font-family: 'Times New Roman', Times, serif; font-size: 17.5px; 
       font-weight: bold; font-style: normal"> 
       Password 
      </td> 
      <td class="style17"> 
       <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="250px" CssClass="Textbox1" MaxLength="15"></asp:TextBox> 
       <asp:RequiredFieldValidator ID="reqPassword" runat="server" ErrorMessage="*" ForeColor="Red" 
        ControlToValidate="txtPassword" Font-Size="Large"></asp:RequiredFieldValidator> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"> 
       <asp:Label ID="lblDisp" runat="server" ForeColor="Red"></asp:Label> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" align="center"> 
       &nbsp;<asp:Button ID="btnSubmit" runat="server" CssClass="btn1" Text="Submit" 
        OnClick="btnSubmit_Click" Width="80px"/> 
       &nbsp;&nbsp; 
       <asp:Button ID="btnCancel" runat="server" CssClass="btn1" Text="Cancel" 
        OnClick="btnCancel_Click" Width="80px"/> 
      </td> 
     </tr> 
    </table> 
</div> 

現在我有以下問題:

  1. 無論用戶類型,總頁面被重定向到Lic_Gen.aspx
  2. 即使我輸入了錯誤的密碼,頁面仍然被重定向到Lic_Gen.aspx
  3. 該頁面還應檢查是否輸入了userType,即AdminCustomer應該與該特定的Login IDPassword一致。但我也不明白。

我知道這些問題正在到達,因爲我已明確將UserType的值定義爲0

當我試圖用下面的代碼轉換它: int userType = Convert.ToInt32(strnew);它不起作用。

那麼有關如何改進我的基本頁面的任何指導?

+1

你有沒有測試是否來自數據庫的值是正確的?請使用F10或F11跟蹤代碼,並使用手錶查看這些值。 – Dev

+0

是。我做到了。我在'strnew'處添加了一個斷點。 User_Type存儲的值即使爲'1',仍由於'UserType = 0'的明確定義而中斷它,並且交換機在情況0時着陸。 – Esha

回答

1

基於提供的代碼,可能出現的問題是:在sdrDatanew

  1. 沒有數據返回,因此userType總是保持0
  2. 數據從DB sdrDatanew["User_Type"]下返回始終爲0(字符串),因此userType始終保持0

請使用以上幾點進一步調試,我看不到代碼中的問題。

小心:您的代碼易受SQL注入攻擊。請閱讀詳情@http://en.wikipedia.org/wiki/SQL_injection。如果你打算在某處發佈這個網站,你肯定會遇到麻煩。

+0

嘿。謝謝你告訴我我落後的地方。如果可能的話,你可以幫助我一點代碼。因爲我對這一切還是陌生的。 – Esha

+0

你需要什麼樣的幫助?我提供了進一步調試的指針。請分享結果,爲我詳細檢查。 – AYK

+0

你會如何將結果存儲在'sdrDatanew'中? – Esha

2

你寫的代碼是錯誤的。即使輸入了錯誤的密碼,它也會重定向,因爲即使沒有從sql查詢返回結果,也會給userType賦值0。此外,我假設查詢沒有返回創建頁面的任何值,以便每次重定向到Lic_Gen.aspx。我建議你看一下ASP會員框架提供所有這些功能,而所有這些代碼...

0

不要使用sdrDatanew.HasRows,只檢查數據讀取器Read()方法:

int userType = -1; 
    if (sdrDatanew.Read()) 
    { 
     userType = Convert.ToInt32(sdrDatanew["User_Type"].ToString()); 
    } 
+0

也AYK說:**您的代碼容易受到SQL注入攻擊**,請使用SqlParameter。 – Behnam

+0

如果我使用sqlParameter,這會防止SQL注入攻擊嗎? – Esha

+0

順便說一下,您的代碼 - 即使輸入正確的登錄ID和密碼 - 顯示「無效的用戶/密碼」錯誤。 – Esha

相關問題