using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

...{
private string strSql;
protected void Page_Load(object sender, EventArgs e)

...{
Response.Write("万能密码再此: '+'tom'--");

}
protected void btnLogin_Click(object sender, EventArgs e)

...{
SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=;");
strSql = "select count(*) from userInfo where userName=" +
"'" + txtUserName.Text + "'" + "and userPwd=" + "'" + txtUserPwd.Text + "'";
con.Open();
SqlCommand cmd = new SqlCommand(strSql, con);
int count = Convert.ToInt32( cmd.ExecuteScalar());
con.Close();
if (count > 0)
lblMessage.Text = "登录成功";
else
lblMessage.Text = "登录失败";
}
}

两次都没输入密码,第一次是普通的登录

第二次利用漏洞进行登录,在用户名文本框中输入 '+'tom'-- ,成功了!
数据库脚本
create table userInfo(
userID int identity(1,1),
userName varchar(20),
userPwd varchar(20)
)
insert into userInfo values('jim','12345')
insert into userInfo values('tom','1111')
insert into userInfo values('jam','ssssss')
insert into userInfo values('suse','888')
HTML代码:

<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align: center">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 73px">
用户名:</td>
<td style="width: 100px">
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 73px">
密码:</td>
<td style="width: 100px">
<asp:TextBox ID="txtUserPwd" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="登 录" Width="69px" />
<asp:Label ID="lblMessage" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>

因此,在代码中直接用SQL进行操作是很危险的