2015-08-27 83 views
1

我有一個登錄頁面,其中我已經編寫代碼來登錄管理部分,但它不工作我不知道什麼問題是這個代碼是正確的仍然未經授權的訪問。幫我出登錄頁面錯誤在asp.net c#

string str = ConfigurationManager.ConnectionStrings["ottscon"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(str)) 
     { 
      SqlCommand cmd = new SqlCommand("Select UserName,Password from login where [email protected] and [email protected]", con); 
      con.Open(); 
      cmd.Parameters.AddWithValue("@userid", TextBox1.ToString()); 
      cmd.Parameters.AddWithValue("@passid", TextBox2.ToString()); 
      SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds= new DataSet(); 
      da.Fill(ds); 
      if (ds.Tables[0].Rows.Count>0) 
      { 

       Session["login"] = TextBox1.Text; 
       Response.Redirect("admintrator123/Default.aspx"); 
      } 
      else 
      { 
       Label1.Text = "Unauthorized Access"; 
       Label1.ForeColor = System.Drawing.Color.Red; 
      } 
     } 
+0

你存儲密碼以純文本形式? – Kamo

+0

是作爲純文本 – shashank

+0

這有點危險,你應該考慮以散列的形式存儲它們。當你直接在數據庫上執行查詢時,你得到的參數會得到任何結果? – Kamo

回答

0

首先使用文本中的文本的值(現有ToString()被返回對象TextBox的類型):

cmd.Parameters.AddWithValue("@userid", TextBox1.Text); 
cmd.Parameters.AddWithValue("@passid", TextBox2.Text); 

嘗試使用這個SQL來檢查記錄存在(內new SqlCommand()):

SELECT CASE WHEN EXISTS (
    SELECT * 
    FROM [login] 
    WHERE [email protected] and [email protected] 
) 
THEN CAST(1 AS BIT) 
ELSE CAST(0 AS BIT) END 

,那麼你可以檢查一個布爾值,而不是一排存及其對WHA更多的瞭解正在發生。

在這之後,你可以讀出的數值是這樣的:

using (var reader = cmd.ExecuteReader()) 
{ 
    while (reader.Read()) 
    { 
     bool exist = reader.GetBoolean(0); 
    } 
} 
+0

這對OP有何幫助?這完全是你建議的一種不同的方法。 –

+0

這將清楚地表明他是否與sql數據有問題。 –

+0

我同意但是這不會解決OP的問題嗎?他將不得不在代碼中完全修改查詢。 –

-2

嘗試

logincommand = "Select UserName,Password from login where UserName='"+TextBox1.Text+"' and Password='"+TextBox2.Text+"'" 
SqlCommand cmd = new SqlCommand(logincommand ,con); 

和刪除

 cmd.Parameters.AddWithValue("@userid", TextBox1.ToString()); 
     cmd.Parameters.AddWithValue("@passid", TextBox2.ToString()); 
+0

SQL注入攻擊! –

+0

這暴露了對SQL注入攻擊的查詢! – Kamo

2

你是不是值傳遞正確

TextBox1.ToString()是錯誤的

使用

TextBox1.Text

+0

仍然無法訪問管理員信息中心 – shashank