Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/1/30 1:58:15
防网站登陆被破解的简单方法_[Asp.Net教程]

防网站登陆被破解的简单方法_[Asp.Net教程]
在大多数的基于数据库的身份认证登陆模块,大多数的程序员只是用一个简单的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;
}
else
{
isCorrect = false;
MessageBox.Show("此管理员用户不存在或者密码错误,请重试", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("操作数据库出错", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
DBAccept.conn.Close();
}

return isCorrect;
}

2.修正版,可正常阻止 1’ or ’1’=’1 登陆
private bool ValidateUser(string LoginId, string LoginPwd)
{
bool isCorrect = false; //定一个bool变量
try
{
DBAccept.conn.Open();

string sql = String.Format("select Password from UserManagement where [UserName]=’{0}’", LoginId);
OleDbCommand command = new OleDbCommand(sql, DBAccept.conn);
if (command.ExecuteScalar().ToString() == LoginPwd)
{
isCorrect = true;
}
else
{
isCorrect = false;
MessageBox.Show("此管理员用户不存在或者密码错误,请重试", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("操作数据库出错", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
DBAccept.conn.Close();
}
return isCorrect;
}


If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759