码迷,mamicode.com
首页 > 数据库 > 详细

sql 登录注入

时间:2015-07-15 12:51:43      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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));
}
}
}

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

sql 登录注入

标签:

原文地址:http://www.cnblogs.com/yangjinwang/p/4647948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!