您的位置:首页 > 编程语言 > ASP

ASP.NET自己写的验证码控件(源代码)

2012-08-28 17:34 405 查看
1,创建一个ASP.NET的服务器控件

2,在服务器控件里面创建一个类,赋值如下

3,生成之后直接在网页拖动使用

4,调用 bool b = this.ValidateNumber1.CheckSn(this.TextBox1.Text);就OK

5,这个只是比较基础的自己弄的玩玩,不过也过得去

--------------------------------源码----------------------------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.IO;

namespace ValidateNumber

{

public class ValidateNumber:Control

{

public void CreateCode() {

//1,定义验证码的颜色,早点,和显示的字符

//颜色列表,用于验证码,噪点,噪线

Color[] color = { Color.Black, Color.Red, Color.Black, Color.Green, Color.Orange, Color.Brown, Color.DarkBlue };

//字体列表,用于验证码

string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiu", "Impact" };

//验证码的字符集,去掉了一些容易混淆的字符

char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };

Random rnd = new Random();

//2,随即生成和保存验证码

string chkCode = "";

//生成验证字符串

for (int i = 0; i < 4; i++)

{

chkCode += character[rnd.Next(character.Length)];

}

//保存验证码采用cookie模式

HttpResponse resp = this.Page.Response;

resp.Cookies["validateCookie"].Value = chkCode;

resp.Cookies["validateCookie"].Expires = DateTime.Now.AddMinutes(2);

//3,绘制噪点,噪线

Bitmap bmp = new Bitmap(100, 30);//长100排序,宽30px的图片

Graphics g = Graphics.FromImage(bmp);

g.Clear(Color.White);

//画五条噪线

for (int i = 0; i < 5; i++)

{

//随机生成噪线的起点和终点

int x1 = rnd.Next(100);

int y1 = rnd.Next(30);

int x2 = rnd.Next(100);

int y2 = rnd.Next(30);

//随机生成噪线的颜色

Color clr = color[rnd.Next(color.Length)];

//画出噪线

g.DrawLine(new Pen(clr), x1, y1, x2, y2);

}

//4,画噪点

for (int i = 0; i < 100; i++)

{

int x = rnd.Next(bmp.Width);

int y = rnd.Next(bmp.Height);

Color clr = color[rnd.Next(color.Length)];

bmp.SetPixel(x, y, clr);

}

//5,画验证字符串

for (int i = 0; i < chkCode.Length; i++)

{

string fnt = font[rnd.Next(font.Length)];

Font ft = new Font(fnt, 16);

Color clr = color[rnd.Next(color.Length)];

g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 20, (float)6);

}

//6,将验证码图片写入内存流,并将其以image/Png格式输出

MemoryStream ms = new MemoryStream();//内存流

try

{

bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

resp.ClearContent();

resp.ContentType = "image/Png";

resp.BinaryWrite(ms.ToArray());

resp.Flush();

resp.End();

}

finally

{

//释放资源

bmp.Dispose();

g.Dispose();

}

}

//7,实现验证方法

public bool CheckSn(string sn) {

return (sn.ToUpper() == this.Page.Request.Cookies["validateCookie"].Value.ToString().ToUpper());

}

//8,创建验证码

protected override void OnInit(EventArgs e)

{

base.OnInit(e);

//开发环境设计视图

if (this.DesignMode) {

return;

}

//如果不是验证码请求

string str=this.Page.Request.QueryString["_ImageTag"];

if (str == "1") {

this.CreateCode();//生成验证码

}

}

protected override void Render(HtmlTextWriter writer)

{

if (!this.DesignMode) { //设计视图

writer.Write("<img border=\"1\" src=\"{0}\" style='width:100px height:30px'>",this.Page.Request.Path+"?_ImageTag=1");

}else{

writer.Write("验证码控件");

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: