页面中:
<table cellSpacing="3" width="100%">
<tr>
<td>用户名</td>
<td><input onkeypress="checkForms(1)" id="unitid" style="WIDTH: 100px" onfocus="this.select();" type="text" maxLength="100"></td>
</tr>
<tr>
<td>密码</td>
<td><input onkeypress="checkForms(2)" id="password" style="WIDTH: 100px" onfocus="this.select();" type="password" maxLength="50">
</td>
</tr>
<tr>
<td align="middle" colSpan="2"><input id="btnLogIn" onclick="LogIn()" type="button" value="登  录">
</td>
</tr>
</table>//javascript脚本
<script language="javascript">
function checkForms(id)
{
if(id == 1)
{
if(event.keyCode == 13)
{
document.all("password").focus();
return false;
}
}
if(id == 2)
{
if(event.keyCode == 13)
{
document.all("btnLogIn").focus();
LogIn();
}
}
}
function LogIn()
{
document.all("btnLogIn").disabled = true;
//用户标识校验
if (document.all("unitid").value == "")
{
alert ("用户标识未填写。");
document.all("unitid").focus();
document.all("btnLogIn").disabled = false;
return;
}
//构造校验字符串
var strCmdLogInPath = "login.aspx";
var strLogInXMLString = "<login><unitid>" + document.all("unitid").value + "</unitid><password>" + document.all("password").value + "</password></login>";
strLogInXMLString = "param=" + escape(strLogInXMLString);
//进行登陆校验
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.Open("POST", strCmdLogInPath, false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.Send(strLogInXMLString);
if (xmlhttp.status == 200)
{
if (unescape(xmlhttp.responseText).toUpperCase() == "TRUE")
{
window.location.href = "default.aspx";
}
else
{
alert ("登陆失败。请检查您输入的用户名与密码是否正确。");
document.all("unitid").focus();

}
}
else
{
alert("登陆校验发生异常。请稍后再试。");
}
document.all("btnLogIn").disabled = false;
}
</script>服务端:string strParam = Request.Form.Get("param");
if(strParam == null)
{
strOutput = "false";
return;
}
XmlDocument doc = new XmlDocument();
try
{
doc.LoadXml(strParam);
string strUnitID = doc.DocumentElement.SelectSingleNode("unitid").InnerText;
if(strUnitID.IndexOf("@") < 0)
{
strOutput = "false";
return;
}
string strPwd = doc.DocumentElement.SelectSingleNode("password").InnerText;
MyClient client = new MyClient();
if(client.IfLoginOK(strUnitID, strPwd))
{
Session["UnitID"] = strUnitID;
Session["UnitName"] = client.GetUnitName(strUnitID);
strOutput = "true";
}
else
{
strOutput = "false";
}
}
catch
{
strOutput = "false";
return;
}//说明:
client.IfLoginOK(strUnitID, strPwd)) //这一行是进行用户登录名与密码的验证
就是在数据库根据这两项进行查询, 如果有数据, 就返回true, 如果没数据, 就false

解决方案 »

  1.   

    1、在提交页面的提交事件判断输入正确后加上:
    Session["username"]="aaaa";
    Session["password"]="bbbb";
    2、在接收页面的Page_Load事件中:
    if(Session["username"]=="aaaa"&&Session["password"]=="bbbb"){
    ....
    Session.RemoveAll();
    }
    else{
    ...
    }
    session是最简便的方法,另外可以用窗体验证,数据库中用户核对等方法。
      

  2.   

    ASP.NET Controls to prevent automatic registrations from bots
    http://www.codeproject.com/aspnet/AntiAuto.asp