我的代码 SQL 是防注入的吗?
Is my code SQL Injection Proof?
这是SQL注入证明吗?或者至少没问题?我在网上下了这个,所以我真的需要一些帮助。我目前正在构建一个相当大的程序,如果我想付费的话,我决定添加一个登录页面。请帮忙!
if (txt_UserName.Text == "" || txt_Password.Text == "")
{
MessageBox.Show("Please provide a Username and Password");
return;
}
try
{
// Create SqlConnection
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName = @username and Password = @password", con);
cmd.Parameters.AddWithValue("@username", txt_UserName.Text);
cmd.Parameters.AddWithValue("@password", txt_Password.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
// If count is equal to 1, than show frmMain form
if (count == 1)
您的代码是 SQL 注入证明。对于Plain SQLQuery,我个人喜欢使用Whosebug中使用的Dapper ORM。
基本相同,但代码少了很多并且returns强类型值而不是 DataSet。
例如,
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
}
User user;
using (IDbConnection cnn = new SqlConnection(cs))
{
user = cnn.Query<User>(
"Select UserName, Password from tbl_Login where UserName=@username and Password=@password",
new { username = txt_UserName.Text, password = txt_Password.Text })
.SingleOrDefault();
}
if (user != null)
{
// Do someting
}
仅供参考:看来您正在存储纯密码。如果是这样,这不是一个好的做法。相反,您想存储加盐的哈希密码。
这是SQL注入证明吗?或者至少没问题?我在网上下了这个,所以我真的需要一些帮助。我目前正在构建一个相当大的程序,如果我想付费的话,我决定添加一个登录页面。请帮忙!
if (txt_UserName.Text == "" || txt_Password.Text == "")
{
MessageBox.Show("Please provide a Username and Password");
return;
}
try
{
// Create SqlConnection
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName = @username and Password = @password", con);
cmd.Parameters.AddWithValue("@username", txt_UserName.Text);
cmd.Parameters.AddWithValue("@password", txt_Password.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
// If count is equal to 1, than show frmMain form
if (count == 1)
您的代码是 SQL 注入证明。对于Plain SQLQuery,我个人喜欢使用Whosebug中使用的Dapper ORM。
基本相同,但代码少了很多并且returns强类型值而不是 DataSet。
例如,
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
}
User user;
using (IDbConnection cnn = new SqlConnection(cs))
{
user = cnn.Query<User>(
"Select UserName, Password from tbl_Login where UserName=@username and Password=@password",
new { username = txt_UserName.Text, password = txt_Password.Text })
.SingleOrDefault();
}
if (user != null)
{
// Do someting
}
仅供参考:看来您正在存储纯密码。如果是这样,这不是一个好的做法。相反,您想存储加盐的哈希密码。