在大多数的基于数据库的身份认证登陆模块,大多数的程序员只是用一个简单的SQL查询语句来实现,这样很容易被用户以简单的( 1' or '1'='1 )查询替换给破解.其实只要稍微的修改一下代码,便可以防止.具体请参看以下两个函数的实现:
以下代码基于C#,数据库为Access
1. 未防止 1' or '1'='1 替换的情况:
private bool ValidateUser(string LoginId, string LoginPwd)
{
bool isCorrect = false;
try
{
DBAccept.conn.Open();
string sql = String.Format("select UserName from UserManagement where [UserName]='{0}' and [Password]='{1}'", LoginId, LoginPwd);
OleDbCommand command = new OleDbCommand(sql, DBAccept.conn);
if (command.ExecuteReader().HasRows)
{
isCorrect = true;