private void btnOK_Click(object sender, System.EventArgs e)
{
//建立与数据源的连接
string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
SqlConnection myConnection = new SqlConnection(sqlconn);
string id=login.Text.ToString();
string pass=Pwd.Text.ToString();
string sql="select * from userreg where login='"+id+"'and password='"+pass+"'";
SqlCommand cmd = new SqlCommand(sql, myConnection);
//打开连接
myConnection.Open();
//读取数据库数据
SqlDataReader rs=cmd.ExecuteReader();
//验证用户的身份
if(rs.Read())
{
//将用户的id值赋值给Session
Session["uid"]=rs["id"].ToString();
Page.Response.Redirect("school.aspx");
}
else
{
lblMsg.Text="用户名或口令错误!请重新输入!";
Pwd.Text="";
}
//关闭数据库连接
myConnection.Close();
}
{
//建立与数据源的连接
string sqlconn = ConfigurationSettings.AppSettings["SQLConnectionString"];
SqlConnection myConnection = new SqlConnection(sqlconn);
string id=login.Text.ToString();
string pass=Pwd.Text.ToString();
string sql="select * from userreg where login='"+id+"'and password='"+pass+"'";
SqlCommand cmd = new SqlCommand(sql, myConnection);
//打开连接
myConnection.Open();
//读取数据库数据
SqlDataReader rs=cmd.ExecuteReader();
//验证用户的身份
if(rs.Read())
{
//将用户的id值赋值给Session
Session["uid"]=rs["id"].ToString();
Page.Response.Redirect("school.aspx");
}
else
{
lblMsg.Text="用户名或口令错误!请重新输入!";
Pwd.Text="";
}
//关闭数据库连接
myConnection.Close();
}
{
SqlConnection conndb = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]); conndb.Open();
string smallclassid=Request.QueryString["bigid"].Trim();
SqlDataAdapter strselectsort = new SqlDataAdapter("prosmallclass_select",conndb); strselectsort.SelectCommand.CommandType = CommandType.StoredProcedure;
strselectsort.SelectCommand.Parameters.Add("@id",smallclassid);
DataSet ds = new DataSet(); strselectsort.Fill(ds);
DlstSort.DataSource=ds; DlstSort.DataKeyField="PdtCat_ID";
DlstSort.DataBind();
}
用带参数的SQL语句:
string sql = "select * from userreg where login=@id and password=@pass";
SqlParameter parID = new SqlParameter("@id",SqlDbType.VARCHAR,30);
SqlParameter parPass = new SqlParameter("@pass",SqlDbType.VARCHAR,30);
cmd.Parameter.Add(parID);
cmd.Parameter.Add(parPass);
建议使用存储过程.
此外,为了安全,登陆时还要限制用户输入等.
楼主的string sql="select * from userreg where login='"+id+"'and password='"+pass+"'";
输入 ' or 1==1 --后
sql的值变成了select * from userreg where login' ' or 1==1 -- 'and....后面东西没用了,SQL中--注释
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。源错误:
行 68:
行 69:
行 70: SqlDataReader rs=cmd.ExecuteReader();
行 71: if(rs.Read())
行 72: {
但是我重新打开浏览器就可以实现这个漏洞
在VS里调试会出错,为什么呢
大哥们帮解释一下