2015-08-27 20 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