以前上网一直难以理解验证码是怎样运作的,电脑怎么能识别图片上的字符呢,这个一直让我很好奇。后来经过了学习,发现验证码其实蛮简单的,但我依旧只了解其中的皮毛,至于内部结构的实现,脑袋里只产生了一点抽象的观念,以至于现在的我只会如何使用验证码,而不知如何去做一个验证码。不过相信以后会理解其中内部结构的实现原理。现在就来写下如何在网站中使用验证码吧!
先上一段验证码的实现代码,慢慢体会其中的精华:
<%@ WebHandler Language="C#" Class="ValidateCode" %>
using System;
using System.Web;
using System.Drawing;
using System.Web.SessionState;
public class ValidateCode : IHttpHandler, IRequiresSessionState
{
HttpContext context;
public void ProcessRequest (HttpContext context1) {
this.context = context1;
CreateCheckCodeImage(GenerateCheckCode());
}
private string GenerateCheckCode()
{
int number;
char code;
string checkCode = String.Empty;
System.Random random = new Random();
for (int i = 0; i < 5; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('0' + (char)(number % 10));
//code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
context.Session.Add("vCode", checkCode);
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
context.Response.ClearContent();
context.Response.ContentType = "image/Gif";
context.Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
public bool IsReusable {
get {
return false;
}
}
}
切记:这段代码是用后缀为一般处理程序实现的
验证码的使用其实也就是对这段代码的调用,已经封装好了的,当然如果能自己做一个验证码的实现,那是很不错的,废话少说,现在开始进行调用。
调用很简单,在头文件中写下javascript代码,封装一个方法,代码如下:
function getVcode(){
document.getElementById("img标签的ID").src="vcode.ashx?w="+new Date();
}
下面是html代码
<img id="vcode" οnclick="getVcode()" src="一般处理程序文件"/>
现在验证码就能显示在网页啦!
在这里当你打开网页的时候验证码实际在session中有了缓存,在网页中建一个文本框,与缓存中的session进行比较,这样一个验证码就实现了!
下面是对输入的验证码的判断正确与否:
//文本框输入的验证码
string userCode = txtCode.Text.Trim();
//判断session是否存在,
if (Session["vCode"] != null)
{
//session没有过期
string sysCode = Session["vCode"].ToString();
if (sysCode == userCode)
{
//验证码输入正确
Context.Response.Write("恭喜,验证码正确!");
}
else
{
//验证码输入错误
Context.Response.Write("对不起,验证码输入错误!");
}
}
else
{
//session过期
Context.Response.Write("验证码以过期!");
}
具体实例:http://download.csdn.net/detail/lsbairll934723180/5382903
本人菜鸟,写下这篇博客让自己学习,以及对验证码有点模糊的同学给点提示,如有不足,大神勿喷,望留下宝贵意见!