创建验证码:ValidateCode.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
public partial class ValidateCode : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
createValidateCode(createValidateStr(3));
}
private string createValidateStr(int n)
{
string strRandom = "";
string str = "a,A,b,B,0,1,2,3,4,5,6,7,8,9,我,你,他";
string[] stra=str.Split(',');//以,分割
//随机获取
Random r = new Random();
for (int i = 0; i < n; i++) {
int k=r.Next(stra.Length);
strRandom += stra[k];
//把验证码放到Session对象里面,就可以在另一个页面使用
Session["str"] = strRandom;
}
return strRandom;
}
public void createValidateCode(string strRandom)
{
int imageWidth = (int)(strRandom.Length * 40);
//画布
Bitmap image= new Bitmap(imageWidth,40);
//创建画图
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Yellow);//更改背景色
//创建画刷
Brush b =new SolidBrush(Color.Red);
//字体
Font f = new Font("楷体", 16,FontStyle.Bold);
//定位
Point p = new Point(10, 10);
//Graphics对象的DrawString方法
g.DrawString(strRandom,f, b, p);
//创建存储流对象
MemoryStream ms = new MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);//iamge的Save方法将该图像保存到指定的流
//设置输出流的类型
Response.ContentType = "image/Jpeg";
//BinaryWrite方法用来将一个二进制字符串写入Http输出流
Response.BinaryWrite(ms.ToArray());
}
}
创建登录页面,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">
.style1
{
height: 37px;
}
.style2
{
height: 53px;
}
.style3
{
height: 47px;
}
.style4
{
height: 50px;
}
.style5
{
height: 43px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style=" border: 2px groove #0000FF; margin: auto; width :400px;">
<table style="width: 100%; height: 320px">
<tr>
<td style="text-align: center; font-size: xx-large;background-color: #C0C0C0; height: 92px;" colspan="2">
用户登录</td>
</tr>
<tr>
<td style="text-align: right; height: 30px; width: 141px;" >
用户名:</td>
<td style="width: 300px; height: 21px; text-align: left;">
<asp:TextBox ID="txtName" runat="server" Width="152px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvNull" runat="server" ControlToValidate="txtName"
ErrorMessage="用户名不能为空" Font-Size="Small"></asp:RequiredFieldValidator></td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
密 码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtPasswd" runat="server" TextMode="Password" Width="151px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt">
<td rowspan="2" style="text-align: right;width: 141px;height: 30px;" >
验证码:</td>
<td style="width: 300px; height: 43px">
<asp:ImageButton ID="IbtnCode" runat="server" ImageUrl="~/ValidateCode.aspx"
OnClick="ImageButton1_Click" />
<asp:Label ID="Label1" runat="server" Font-Size="Small" ForeColor="Blue" Text="看不清?点击换一张"></asp:Label></td>
</tr>
<tr style="font-size: 12pt">
<td style="width: 300px; height: 43px; text-align: left;">
<asp:TextBox ID="txtCode" runat="server" Width="139px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt">
<td style=" height: 27px;">
</td>
<td style="width: 300px; height: 27px;">
<asp:Button ID="btnOK" runat="server" Text="登录" OnClick="Button1_Click" />
<asp:Button ID="btnClear" runat="server" Text="重置" OnClick="btnClear_Click" />
</td>
</tr>
<tr style="font-size: 12pt" >
<td colspan="2" style="background-color: #CCCCCC" >
<a href="Regedit.aspx" style="font-size:12px">注册新用户</a>
<a href="changePswd.aspx" style="font-size:12px">修改密码</a>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
设计:
Login.aspx.cs,功能代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
IbtnCode.ImageUrl = "~/ValidateCode.aspx";
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPasswd.Text = "";
txtCode.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string pswd = txtPasswd.Text.Trim();
string code = txtCode.Text.Trim();
string str = "server=.;database=StudentSystem;integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
str = "select count(*) from userTable1 where userID='" + name + "' and userPsd='" + pswd + "'";
SqlCommand cmd = new SqlCommand(str, con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0 && ((Session["str"].ToString() == code)))
{
Response.Write("<script>alert('登录成功');</script>");
Response.Redirect("Sucess.aspx");
}
else if (Session["str"].ToString() != code)
{
Response.Write("<script>alert('验证码不正确,请检查输入信息');</script>");
}
else
{
Response.Write("<script>alert('登录不成功,请检查输入信息');</script>");
}
}
}
成功页面Sucess.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Sucess.aspx.cs" Inherits="Sucess" %>
<!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>
恭喜你,登录成功!
<a href ="Login.aspx">返回</a> 登录页面 </div>
</form>
</body>
</html>
Regiedit.aspx,注册页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Regedit.aspx.cs" Inherits="Regedit" %>
<!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=" border: 2px groove #0000FF; margin: auto; width :400px;">
<table style="width: 100%; height: 320px">
<tr>
<td style="text-align: center; font-size: xx-large;background-color: #C0C0C0; height: 92px;" colspan="2">
用户注册</td>
</tr>
<tr>
<td style="text-align: right; height: 30px; width: 141px;" >
用户名:</td>
<td style="width: 300px; height: 21px; text-align: left;">
<asp:TextBox ID="txtName" runat="server" Width="152px"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvNull" runat="server" ControlToValidate="txtName"
ErrorMessage="不能为空" Font-Size="Small" ForeColor="Red"></asp:RequiredFieldValidator></td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
密 码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtPswd" runat="server" TextMode="Password" Width="151px"></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
确认密码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtPswd2" runat="server" TextMode="Password" Width="151px"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPswd"
ControlToValidate="txtPswd2" ErrorMessage="密码不一致" Font-Size="10pt" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
QQ:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtQQ" runat="server" Width="151px" ></asp:TextBox></td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
电子邮件:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtEmail" runat="server" Width="151px" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail"
ErrorMessage="格式不对" Font-Size="10pt" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ForeColor="Red"></asp:RegularExpressionValidator>
</td>
</tr>
<tr style="font-size: 12pt">
<td style=" height: 27px;">
</td>
<td style="width: 300px; height: 27px;">
<asp:Button ID="btnOK" runat="server" Text="注册" OnClick="Button1_Click" />
<asp:Button ID="btnClear" runat="server" Text="重置" OnClick="btnClear_Click" />
</td>
</tr>
<tr style="font-size: 12pt" >
<td colspan="2" style="background-color: #CCCCCC ;font-size: small;" >
<a href ="Login.aspx">返回</a></td>
</tr>
</table>
</div>
</form>
</body>
</html>
设计:
Regedit.aspx.cs,功能代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Regedit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string pswd = txtPswd.Text.Trim();
string pswd2 = txtPswd2.Text.Trim();
string qq = txtQQ.Text.Trim();
string email = txtEmail.Text.Trim();
try
{
//连接数据库
string str = "server=.;database=StudentSystem;integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
//执行数据库操作
str = "insert into usertable1 values('" + name + "','" + pswd + "','" + qq + "','" + email + "')";
SqlCommand cmd = new SqlCommand(str, con);
if (cmd.ExecuteNonQuery()>0)
{
Response.Write("<script>alert('注册成功');window.open('Login.aspx');</script>");
}
}
catch
{
Response.Write("<script>alert('注册不成功,请检查输入信息');</script>");
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtPswd.Text = "";
txtPswd2.Text = "";
txtQQ.Text = "";
txtEmail.Text = "";
}
}
修改密码页面,changPswd.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="changePswd.aspx.cs" Inherits="changePswd" %>
<!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=" border: 2px groove #0000FF; margin: auto; width :400px;">
<table style="width: 100%; height: 280px">
<tr>
<td style="text-align: center; font-size: xx-large;background-color: #C0C0C0; height: 92px;" colspan="2">
修改密码</td>
</tr>
<tr>
<td style="text-align: right; height: 30px; width: 141px;" >
用户名:</td>
<td style="width: 300px; height: 21px; text-align: left;">
<asp:TextBox ID="txtName" runat="server" Width="152px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName"
ErrorMessage="*" Font-Size="10pt" ForeColor="Red"></asp:RequiredFieldValidator></td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
旧密码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtOldPswd" runat="server" TextMode="Password" Width="151px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtOldPswd"
ErrorMessage="*" Font-Size="10pt" ForeColor="Red"></asp:RequiredFieldValidator>
</td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
新密码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtNewPswd" runat="server" TextMode="Password" Width="151px"></asp:TextBox>
</td>
</tr>
<tr style="font-size: 12pt; color: #000000">
<td style="text-align: right; height: 30px; width: 141px;" >
确认密码:</td>
<td style="width: 300px; height: 18px; text-align: left;">
<asp:TextBox ID="txtPswd2" runat="server" Width="151px" TextMode="Password" ></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtNewPswd"
ControlToValidate="txtPswd2" ErrorMessage="密码不一致" Font-Size="10pt" ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
<tr style="font-size: 12pt">
<td style=" height: 27px;">
</td>
<td style="width: 300px; height: 27px;">
<asp:Button ID="btnOK" runat="server" Text="修改" OnClick="Button1_Click" />
<asp:Button ID="btnClear" runat="server" Text="重置" OnClick="btnClear_Click" />
</td>
</tr>
<tr style="font-size: 12pt" >
<td colspan="2" style="background-color: #CCCCCC ;font-size: small;" >
<a href ="Login.aspx">返回</a></td>
</tr>
</table>
</div>
</form>
</body>
</html>
设计:
changPswd.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class changePswd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string oldpswd = txtOldPswd.Text.Trim();
string newpswd = txtNewPswd.Text.Trim();
string pswd = txtPswd2.Text.Trim();
try
{
//连接数据库
string str = "server=.;database=StudentSystem;integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
//执行数据库操作,检查用户名和密码是否匹配
str = "select count(*) from usertable1 where userID='" + name + "' and userPsd='" + oldpswd + "'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = str;
int count = Convert.ToInt32(cmd.ExecuteScalar());
//当存在此用户,且密码和用户名正确时
if (count > 0)
{
//更新数据库表中的密码
str = "update usertable1 set userPsd='" + newpswd + "'where userID='" + name + "' ";
cmd.CommandText = str;
cmd.ExecuteNonQuery();
Response.Write("<script>alert('修改密码成功');window.open('Login.aspx');</script>");
}
else
{
Response.Write("<script>alert('没有此用户或用户密码不匹配,请重新输入');</script>");
txtName.Text = "";
txtName.Focus(); // 鼠标定位在txtName控件
}
}
catch
{
Response.Write("<script>alert('修改密码不成功,请检查输入信息');</script>");
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtOldPswd.Text = "";
txtPswd2.Text = "";
txtNewPswd.Text = "";
}
}