在网站上,我们经常看到随机数,让用户去输入,之后进行验证。下面用C#语言,做一个随机验证码。
1.做一个页面,左边是个文本框,右边是个图片,如果文本框输入的数字和右边的数字不同,则验证不通过,以下是HTML语言的代码:
这是HTML的部分代码,不是整个代码
此代码,主要工作是,左边是个文本框,右边是个图片,
对输入的数字进行验证
< td colspan ="3" bgcolor ="#b5dae3" >
< asp:TextBox ID ="txtCode" runat ="server" Height ="13px" Width ="61px" AutoCompleteType ="Disabled" ></ asp:TextBox ></ td >
< td colspan ="2" bgcolor ="#b5dae3" align ="left" >
< img height ="20" src ="ICode.aspx" width ="40" alt ="" /></ td >
此代码,主要工作是,左边是个文本框,右边是个图片,
对输入的数字进行验证
< td colspan ="3" bgcolor ="#b5dae3" >
< asp:TextBox ID ="txtCode" runat ="server" Height ="13px" Width ="61px" AutoCompleteType ="Disabled" ></ asp:TextBox ></ td >
< td colspan ="2" bgcolor ="#b5dae3" align ="left" >
< img height ="20" src ="ICode.aspx" width ="40" alt ="" /></ td >
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class ArtDesign_LogTest : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void ImgLogin_Click(object sender, ImageClickEventArgs e)
...{
//进行验证码的操作
if (Session["ICode"] == null)
...{
Response.Redirect("Login.aspx");
}
if (this.txtCode.Text != Session["ICode"].ToString())
...{
//不能转向另外一页
WebClass.Define.MessageBox(this.Page, "验证码错误!");
this.txtCode.Text = "";
this.txtPwd.Text = "";
return;
}
//。。。。。。。
}
protected void ImageRegister_Click(object sender, ImageClickEventArgs e)
...{
Response.Redirect("Register/Reg_Enterprise.aspx");
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class ArtDesign_LogTest : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
}
protected void ImgLogin_Click(object sender, ImageClickEventArgs e)
...{
//进行验证码的操作
if (Session["ICode"] == null)
...{
Response.Redirect("Login.aspx");
}
if (this.txtCode.Text != Session["ICode"].ToString())
...{
//不能转向另外一页
WebClass.Define.MessageBox(this.Page, "验证码错误!");
this.txtCode.Text = "";
this.txtPwd.Text = "";
return;
}
//。。。。。。。
}
protected void ImageRegister_Click(object sender, ImageClickEventArgs e)
...{
Response.Redirect("Register/Reg_Enterprise.aspx");
}
}
我们从上面给出了HTML代码中,我们可以看出,图片的SRC属性是ICode.aspx网页,也就是说ICode.aspx网页会返回它的路径,以下是ICode.aspx的后台代码,此页面,主要进行一些处理,因此在此页面没有加任何的控件。
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Drawing.Text;
using System.Drawing;
public partial class Compact_ICode : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
Random random = new Random();
string Code = random.Next(0, 9).ToString() + random.Next(0, 9).ToString() + random.Next(0, 9).ToString() + random.Next(0, 9).ToString();
string RanInt = Code;
Session["ICode"] = RanInt;
Response.ContentType = "image/gif";
this.getImage(RanInt).Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
Response.End();
}
private System.Drawing.Bitmap getImage(string s)
...{
Bitmap b = new Bitmap(1, 1);
int width, height;
Font aFont = new Font("Times new Roman", 24, System.Drawing.GraphicsUnit.Point);
Graphics gr = Graphics.FromImage(b);
width = (int)gr.MeasureString(s, aFont).Width;
height = (int)gr.MeasureString(s, aFont).Height;
b = new Bitmap(b, new Size(width, height));
gr=Graphics.FromImage(b);
Random nrandom = new Random();
gr.Clear(Color.Blue);
//int i;
//for (i = 0; i < 4; i++)
//{
// gr.DrawLine(new Pen(Color.Yellow), new Point(nrandom.Next(0, width), nrandom.Next(0, height)), new Point(nrandom.Next(0, width), nrandom.Next(0, height)));
//}
int bcolor=nrandom.Next(0,255);
gr.DrawString(s,aFont,new SolidBrush(Color.Yellow),0,0);
gr.Flush();
return b;
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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.Drawing.Text;
using System.Drawing;
public partial class Compact_ICode : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
Random random = new Random();
string Code = random.Next(0, 9).ToString() + random.Next(0, 9).ToString() + random.Next(0, 9).ToString() + random.Next(0, 9).ToString();
string RanInt = Code;
Session["ICode"] = RanInt;
Response.ContentType = "image/gif";
this.getImage(RanInt).Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
Response.End();
}
private System.Drawing.Bitmap getImage(string s)
...{
Bitmap b = new Bitmap(1, 1);
int width, height;
Font aFont = new Font("Times new Roman", 24, System.Drawing.GraphicsUnit.Point);
Graphics gr = Graphics.FromImage(b);
width = (int)gr.MeasureString(s, aFont).Width;
height = (int)gr.MeasureString(s, aFont).Height;
b = new Bitmap(b, new Size(width, height));
gr=Graphics.FromImage(b);
Random nrandom = new Random();
gr.Clear(Color.Blue);
//int i;
//for (i = 0; i < 4; i++)
//{
// gr.DrawLine(new Pen(Color.Yellow), new Point(nrandom.Next(0, width), nrandom.Next(0, height)), new Point(nrandom.Next(0, width), nrandom.Next(0, height)));
//}
int bcolor=nrandom.Next(0,255);
gr.DrawString(s,aFont,new SolidBrush(Color.Yellow),0,0);
gr.Flush();
return b;
}
}
这样随机数就生成了。
当刷新一下页面,会出现新的随机数:
以下是刷新后的页面截图: