sql 登录注入

DataTable dt= SqlHelper.ExecuteDataTable(System.Data.CommandType.Text, String.Format("select * from Automation_User where userName='{0}' and password='{1}'", userName, password), null);

如果是拼接式sql登录的话很容易进行sql注入

'or'1'='1

 

所以登录的sql尽量写成参数化,

using (SqlConnection cnn = new SqlConnection(con))
{
using (SqlCommand cmd = cnn.CreateCommand())
{
try
{
cmd.CommandText = "select * from User where username=@username and password=@password";
cmd.Parameters.Add(new SqlParameter("@username", username));
cmd.Parameters.Add(new SqlParameter("@password", password));
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
cnn.Close(); //记得要加上 关闭
if (ds.Tables[0].Rows.Count > 0)
{
type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
}
}
catch (Exception ex)
{
// type = -2;
logInstance.Info(string.Format("登录出现异常,异常信息为:{0}",ex.Message));
}
}
}

//参数化时,单引号和双引号都会被转义

posted @ 2015-07-15 12:05  秋香姑娘请你不要紧张  阅读(330)  评论(0编辑  收藏  举报