前台代码:

<head>
<title></title>
<styletype="text/css">
body{font:12pxHelvetica,Arial,sans-serif;margin:100pxauto;padding:0;word-wrap:break-word;word-break:break-all;background-color:#016BA9;}
.login1{background-p_w_picpath:url(../Images/login_1.jpg);width:960px;height:94px;margin:0auto;}
.login2{background-p_w_picpath:url(../Images/login_2.jpg);width:960px;height:49px;margin:0auto;}
.login3{background-p_w_picpath:url(../Images/login_3.jpg);width:960px;height:125px;margin:0auto;}
.login4{background-p_w_picpath:url(../Images/login_4.jpg);width:960px;height:91px;margin:0auto;}
.loginTXT{border:1pxsolid#800000;width:150px;background-color:#866961;color:#fff;height:19px;}
</style>
<scripttype="text/javascript">
if(top.location!=self.location)
top.location=self.location;

functionCheckIn(){
varnames=document.getElementById("txtU_Name").value;
varpass=document.getElementById("txtU_Pass").value;
varcheck=document.getElementById("txtU_Check").value;
if(names==""){
alert("请输入用户名...");
document.getElementById("txtU_Name").focus();
returnfalse;
}
elseif(pass==""){
alert("请输入用户密码...");
document.getElementById("txtU_Pass").focus();
returnfalse;
}
elseif(check==""){
alert("请输入验证码...");
document.getElementById("txtU_Check").focus();
returnfalse;
}
}
</script>
</head>
<body>
<formid="form1"runat="server"defaultfocus="txtU_Name"defaultbutton="BtnLogin">
<divclass="login1"></div>
<divclass="login2"></div>
<divclass="login3">
<tablecellpadding="0"cellspacing="1"border="0"style="margin-left:320px;width:300px;">
<tr>
<td>用户名称:</td>
<td><asp:textboxID="txtU_Name"runat="server"class="loginTXT"maxlength="20"></asp:textbox></td>
</tr>
<tr>
<td>用户密码:</td>
<td><inputid="txtU_Pass"type="password"runat="server"class="loginTXT"maxlength="6"/></td>
</tr>
<tr>
<td>验证代码:</td>
<td><asp:textboxID="txtU_Check"runat="server"class="loginTXT"style="width:85px;"maxlength="5"/>&nbsp;<imgsrc="../VateImage.ashx"onclick="this.src='../VateImage.ashx?'+newDate().getTime()"alt="看不清楚再换一张!"style="vertical-align:middle;"/></td>
</tr>
<tr>
<tdcolspan="2"><hr/></td>
</tr>
<tr>
<td></td>
<td>
<asp:buttonID="BtnLogin"runat="server"text="登录管理"OnClientClick="returnCheckIn()"onclick="BtnLogin_Click"/>
&nbsp;<asp:buttonID="BtnHome"runat="server"text="返回首页"
onclick="BtnHome_Click"/>
</td>
</tr>
</table>
</div>
<divclass="login4"></div>
</form>
</body>
</html>

后台代码:

protectedvoidBtnLogin_Click(objectsender,EventArgse)
{
stringusername=txtU_Name.Text;
stringpwd=txtU_Pass.Value;
stringcheck=txtU_Check.Text;

if(CheckNull())
{
if(check.ToString().ToLower()==Session["ValidateCode"].ToString().ToLower())
{
stringstrsql="selectcount(1)fromLoginwhereUserName='"+username+"'andUserPwd='"+pwd+"'";
objectobj=SqlHelper.ExecuteScalar(ConfigurationManager.ConnectionStrings["conn"].ToString(),CommandType.Text,strsql);
if(obj!=null&&(int)obj>0)
{
Response.Write("<script>alert('登录成功!')</script>");
Session["username"]=username;

HttpCookiehc=newHttpCookie("username",username);
hc.Expires=DateTime.Now.AddMinutes(1);
Response.Cookies.Add(hc);

Response.Redirect("Main.aspx");
}
else
{
Response.Write("<script>alert('用户名或密码错误!')</script>");
Response.Write("<script>window.location.href='Login.aspx'</script>");
}
}
else
{
Response.Write("<script>alert('验证码错误!')</script>");
Response.Write("<script>window.location.href='Login.aspx'</script>");

}
}


}
privateboolCheckNull()
{
return(!(string.IsNullOrEmpty(txtU_Name.Text.Trim()))&&!(string.IsNullOrEmpty(txtU_Pass.Value.Trim()))&&!(string.IsNullOrEmpty(txtU_Check.Text.Trim())))?true:false;
}

main主页代码:page_Load

try
{
stringusername=Session["username"].ToString();
}
catch
{
Response.Write("<script>alert('请从主页面登录!');location.href='Login.aspx';</script>");
}