Login.aspx前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>
<!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>
<style type="text/css">
body
{
position: relative;
width: 30%;
margin: 0 auto;
text-align: center;
margin-top: 105px;
color: #606060;
font-size: 12px;
height :100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table width="385" height="220" background="../Images/User_Login_206.gif">
<tr>
<td height="54">
</td>
</tr>
<tr>
<td align="right" width="120" height="30">
用户名:
</td>
<td style="height: 5px">
<asp:TextBox ID="username" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*姓名必填"
ControlToValidate="username"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" height="30px">
密 码:
</td>
<td>
<asp:TextBox ID="pwd" runat="server" TextMode="Password" Width="150px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*密码必填"
ControlToValidate="pwd"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right" height="30px">
验证码:
</td>
<td>
<asp:TextBox ID="checkCode" runat="server" Width="150px"></asp:TextBox>
</td>
<td style="color: Blue; text-align: left;">
<div style="background-color: #ececec; width: 35px; height: 20px; border: solid 1px #6AC0FF;">
<asp:Label ID="verifyCode" runat="server"></asp:Label></div>
</td>
</tr>
</table>
<br />
<p style="width: 385px">
<asp:Button ID="submit" runat="server" Text="登录" OnClick="submit_Click" Width="74px" />
<asp:Button ID="cancel" runat="server" Text="取消" OnClick="cancel_Click" Width="74px" />
</p>
</form>
</body>
</html>
后台代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
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;
using System.Xml;
public partial class Login : System.Web.UI.Page
{
XmlDocument xd = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.verifyCode.Text = Convert.ToString(GetCode());
}
}
public int GetCode()
{
Random rand = new Random();
int code = rand.Next(1000, 9999);
return code;
}
protected void submit_Click(object sender, EventArgs e)
{
string name = this.username.Text.Trim();
string password=this.pwd .Text .Trim ();
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("User.xml"));
DataTable dt = ds.Tables[0];
DataRow[] dta = dt.Select("Name='" + name+ "'");
if (dta != null && dta.Length > 0)
{
DataRow dr = dta[0];
string strPwd = (string)dr["Password"];
if (strPwd == password && this.checkCode.Text.Trim() == this.verifyCode.Text.Trim())
{
Response.Redirect("Index.aspx");
}
else
{
Response.Write("<script>alert('输入的密码或验证码息错误!')</script>");
}
}
else
{
Response.Write("<script>alert('输入的姓名和密码错误!请重新输入')</script>");
}
}
protected void cancel_Click(object sender, EventArgs e)
{
Response.Write("<script>window.close();location='javascript:history.go(-1)';</script>");
}
}
user.xml的文件内容
<?xml version="1.0" encoding="utf-8"?>
<Users>
<ID number="1">
<Num>1</Num>
<Name>wjn</Name>
<Password>111</Password>
<Date>2012-07-26</Date>
</ID>
<ID number="2">
<Num>2</Num>
<Name>admin</Name>
<Password>admin</Password>
<Date>2012-07-26</Date>
</ID>
<ID number="3">
<Num>3</Num>
<Name>56565</Name>
<Password>4555</Password>
<Data>2012-02-29</Data>
</ID>
<ID number="4">
<Num>4</Num>
<Name>aaa</Name>
<Password>222</Password>
<Data>2012-07-26</Data>
</ID>
<ID number="5">
<Num>5</Num>
<Name>bbb</Name>
<Password>111</Password>
<Data>2012-07-26</Data>
</ID>
</Users>