VerifyCode.aspx.cs
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Response.Buffer = true;
string checkCode = CreateRandomCode(6);
Session["CheckCode"] = checkCode;
Session.Timeout = 1;
CreateImage(checkCode);
}
Web 窗体设计器生成的代码
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" ;
string[] allCharArray = allChar.Split(\',\');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void CreateImage(string checkCode)
{
Response.Expires = -1;
Response.AddHeader ("Pragma","no-cache");
Response.AddHeader ("cache-ctrol","no-cache");
int iwidth = (int)(checkCode.Length * 19);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 35);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);
Brush b = new System.Drawing.SolidBrush(Color.RoyalBlue);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.AliceBlue);
g.DrawString(checkCode, f, b, 5, 5);
Pen blackPen = new Pen(Color.RoyalBlue, 0);
Random rand = new Random();
//随机产生的竖条
for (int i=0;i<5;i++)
{
int x = rand.Next(image.Width);
g.DrawLine(blackPen,x,0,x,image.Height);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
{
// 在此处放置用户代码以初始化页面
Response.Buffer = true;
string checkCode = CreateRandomCode(6);
Session["CheckCode"] = checkCode;
Session.Timeout = 1;
CreateImage(checkCode);
}
Web 窗体设计器生成的代码
private string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z" ;
string[] allCharArray = allChar.Split(\',\');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for(int i = 0; i < codeCount; i++)
{
if(temp != -1)
{
rand = new Random(i*temp*((int)DateTime.Now.Ticks));
}
int t = rand.Next(61);
if(temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void CreateImage(string checkCode)
{
Response.Expires = -1;
Response.AddHeader ("Pragma","no-cache");
Response.AddHeader ("cache-ctrol","no-cache");
int iwidth = (int)(checkCode.Length * 19);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 35);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font("Arial", 18, System.Drawing.FontStyle.Regular);
Brush b = new System.Drawing.SolidBrush(Color.RoyalBlue);
//g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);
g.Clear(Color.AliceBlue);
g.DrawString(checkCode, f, b, 5, 5);
Pen blackPen = new Pen(Color.RoyalBlue, 0);
Random rand = new Random();
//随机产生的竖条
for (int i=0;i<5;i++)
{
int x = rand.Next(image.Width);
g.DrawLine(blackPen,x,0,x,image.Height);
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
调用以及验证
验证码:<asp:TextBox id="VerifyCode" Width="50" runat="server"></asp:TextBox>
<asp:Image id="VerifyCode2" ImageAlign="AbsMiddle" ImageUrl="VerifyCode.aspx" runat="server"></asp:Image>
<asp:Image id="VerifyCode2" ImageAlign="AbsMiddle" ImageUrl="VerifyCode.aspx" runat="server"></asp:Image>
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string checkCode;
if(Session["CheckCode"]!= null)
{
checkCode = Session["CheckCode"].ToString();
Session.Abandon();
}
else
{
checkCode = "99999"; //验证码过期
}
ViewState["CheckCode"] = checkCode;
}
Web 窗体设计器生成的代码
private void btn_Reg_Click(object sender, System.EventArgs e)
{
if (VerifyCode.Text.Trim() != ViewState["CheckCode"].ToString())
{
if (ViewState["CheckCode"].ToString() == "99999")
{
Response.Write("<script>alert(\"由于您放置时间过长,验证码失效!请刷新后输入!\");location=\'Login.aspx\'</script>");
}
else
{
Response.Write("<script>alert(\"您输入的验证码不正确!请重新输入!\");location=\'Login.aspx\'</script>");
}
}
else
{
.......
}
}
}
{
// 在此处放置用户代码以初始化页面
string checkCode;
if(Session["CheckCode"]!= null)
{
checkCode = Session["CheckCode"].ToString();
Session.Abandon();
}
else
{
checkCode = "99999"; //验证码过期
}
ViewState["CheckCode"] = checkCode;
}
Web 窗体设计器生成的代码
private void btn_Reg_Click(object sender, System.EventArgs e)
{
if (VerifyCode.Text.Trim() != ViewState["CheckCode"].ToString())
{
if (ViewState["CheckCode"].ToString() == "99999")
{
Response.Write("<script>alert(\"由于您放置时间过长,验证码失效!请刷新后输入!\");location=\'Login.aspx\'</script>");
}
else
{
Response.Write("<script>alert(\"您输入的验证码不正确!请重新输入!\");location=\'Login.aspx\'</script>");
}
}
else
{
.......
}
}
}