我目前有一个网站,具有正常的注册和登录,用ASP.net编码。 我正在使用Access数据库,而我的朋友使用C#类来处理大部分数据库操作(executeQuery,executeRead,isExits ...)。使用ASP.net和Access数据库从SQL注入安全的网站
现在我几乎完成了构建我的网站,我想开始添加安全性 - 主要是对我的数据库。我已经搜索了一段时间的关于这个主题的教程,但是我找不到任何好的exept旧的microsoft msdn文章,我不能真正地让它的代码工作。 我现在拥有的最远的地方就是不允许在用户名和密码中使用任何危险字符(例如', - ,;),但它有种感觉,好像它是我可以使用的更糟糕的解决方案(为什么我的用户不应该使用这些字符?)。
我认为我找到的最好的解决方案是在声明后将变量插入到查询字符串中(与“WHERE username = @ user”或类似的东西有关),但我无法获得它与Access和我的oleDBManager一起使用。
这是我目前的注册码。 handle()从字符串中删除所有',并且Validate()检查字符串中的危险部分。
string username = user.Text;
string password = pass.Text;
bool isThingy = false;
if (handle(ref password)) isThingy = true;
if (handle(ref username)) isThingy = true;
if (username != "" && username != null)
{
if (password != "" && password != null)
{
if (Validate(username, password))
{
if ((db.IsExist("SELECT * FROM Table1 WHERE username='" + username + "'") == false))
{
int a = db.ExecuteQuery("INSERT INTO `Table1`(`username`, `password`, `logins`, `email`, `fname`, `lname`, `country`, `city`, `birthday`, `userid`) VALUES ('" + username + "', '" + password + "', '0', '', '', '', '', '', '', '" + Convert.ToString(Convert.ToInt32(db.ExecuteCellRead("SELECT MAX(userid) FROM Table1")) + 1) + "');");
if (!isThingy) errorLabel.Text = "Your user has been successfully registered";
else errorLabel.Text = "The ' token is invalid. your user was registered absence the '.";
}
else
errorLabel.Text = "This username is already taken";
}
else errorLabel.Text = "Invalid name format";
}
else errorLabel.Text = "Please enter a password";
}
else errorLabel.Text = "Please enter a user name";
作为oleDBManager(在我的代码命名为DB):
private OleDbConnection link; // The link instance
private OleDbCommand command; // The command object
private OleDbDataReader dataReader; // The data reader object
private OleDbDataAdapter dataAdapter; // the data adapter object
private DataTable dataTable; // the data table object
private string dbName; // the Database filename
private int version; // the usersTableG office version
private string connectionString; // the connection string for the database connection
private string provider; // the matching driver string for the connection string
private string path; // the path to the database file
...
public int ExecuteQuery(string query)
{
this.link.Open();
int rowsAffected;
// ---
this.command = new OleDbCommand(query, this.link);
try
{
rowsAffected = this.command.ExecuteNonQuery();
}
catch (InvalidOperationException e)
{
if (e.Data == null)
throw;
else
rowsAffected = -1;
}
finally
{
this.command.Dispose();
this.link.Close();
}
// ---
return rowsAffected;
}
public bool IsExist(string query)
{
this.link.Open();
// ---
this.command = new OleDbCommand(query, this.link);
this.dataReader = this.command.ExecuteReader();
bool a = this.dataReader.Read();
// ---
this.command.Dispose();
this.link.Close();
// ---
return a;
}
public string ExecuteCellRead(string query)
{
string output = "";
this.dataTable = this.ExcecuteRead(query);
foreach (DataRow row in this.dataTable.Rows)
{
foreach (object obj in row.ItemArray)
{
output += obj.ToString();
}
}
return output;
}
所以,你可能会看到,主要的问题是,用户现在不能使用字符”。 它假设最好的解决方案是在SQL查询中使用@变量,但我不知道如何。
[感谢您的帮助] PS。我已经改变了我的表格的名称;)
编辑:你们大多数人告诉我要使用这些参数化查询,但如果你能给我一个如何使用它们的例子,做到这一点
所以,感谢@Remou,我FINAL代码:
db.DoWeirdStackOverFlowStuff(
"INSERT INTO `Table1`(`username`, `password`, `logins`) VALUES (@username, @password, '0');"
, new string[] { "@username", "@password" }
, new string[] { username, password });
和
public int DoWeirdStackOverFlowStuff(string query, string[] vars, string[] reps)
{
this.link.Open();
int rowsAffected;
// ---
this.command = new OleDbCommand();
this.command.CommandText = query;
this.command.CommandType = System.Data.CommandType.Text;
this.command.Connection = this.link;
//Parameters in the order in which they appear in the query
for (int i = 0; i < vars.Length; i++)
this.command.Parameters.AddWithValue(vars[i], reps[i]);
try
{
rowsAffected = this.command.ExecuteNonQuery();
}
catch (InvalidOperationException e)
{
if (e.Data == null)
throw;
else
rowsAffected = -1;
}
finally
{
this.command.Dispose();
this.link.Close();
}
// ---
return rowsAffected;
}
用于任何需要这种=]
Visual Studio和在线MSDN都有参数化的sql命令的简单示例。 – Igor 2013-03-14 16:05:12
这里是SQL注入预防的介绍https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet(我知道它不是.NET特定的,但校长是相同的) – BLSully 2013-03-14 16:06:07
使用参数化的sql命令。黑名单字符不会阻止你从sql注入。你应该只在你的数据库中保存密码哈希值。不是他们自己的密码。并用盐来保护它们。 – Peter 2013-03-14 16:09:59