怎么使用ASP.NET实现生成验证码功能

  介绍

这篇文章给大家分享的是有关怎么使用ASP.NET实现生成验证码功能的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

生成验证码原理:产生随机字符,并将字符生成为图片,同时储存到会话里去,然后验证用户输入的内容是否与会话中的验证码相符即可。

一般处理程序:CheckCodeHandler。cs

& lt; % @ WebHandler语言=癱#“;类=癈heckCodeHandler"%比;   使用系统;   使用包含;   使用text;   使用System.Drawing;   使用System.Web.SessionState;   公共类CheckCodeHandler: IHttpHandler IRequiresSessionState   {//产生验证码的字符集   公共字符串charcode=?, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, J, K、M, N, P, R, S, U, W, X, Y, A, B, C, D, E, F, G, H, J, K、M, N, P, R, S, U, W, X, y";   公共空间ProcessRequest (HttpContext上下文){   字符串validateCode=CreateRandomCode (4);   context.Session [“ValidateCode"]=validateCode;//将验证码保存到会话中   CreateCodeImage (validateCode、上下文);   }   公共bool IsReusable {   得到{   返回错误;   }   }///& lt; summary>///生成验证码///& lt;/summary>///& lt;参数name=皀"在验证码个数& lt;/param>///& lt; returns>验证码字符串& lt;/returns>   公共字符串CreateRandomCode (int n)   {   string [] CharArray=charcode.Split(& # 39; & # 39;);//将字符串转换为字符数组   字符串randomCode=?“;   int temp=1;   随机兰德=new随机();   for (int i=0;我& lt;n;我+ +)   {   如果(临时!=1)   {   兰德=new随机(我*临时* ((int) DateTime.Now.Ticks));   }   int t=rand.Next (CharArray。长度- 1);   如果(临时!=1,,temp==t)   {   返回CreateRandomCode (n);   }   temp=t;   randomCode +=CharArray [t];   }   返回randomCode;   }   公共空间CreateCodeImage(字符串checkCode HttpContext上下文)   {   int iwidth=(int) (checkCode。长度* 13);   System.Drawing。位图图像=新System.Drawing。位图(iwidth 20);   图g=Graphics.FromImage(图片);   字体f=新System.Drawing.Font (“Arial" 12 (System.Drawing.FontStyle。斜体| System.Drawing.FontStyle.Bold));//前景色   刷b=new System.Drawing.SolidBrush (Color.Black);//背景色   g.Clear (Color.White);//填充文字   g。拉带(checkCode, f, b, 0,1);//随机线条   笔linePen=新钢笔(颜色。灰色,0);   随机兰德=new随机();   for (int i=0;我& lt;5;我+ +)   {   int x1=rand.Next (image.Width);   int y?=rand.Next (image.Height);   int x2=rand.Next (image.Width);   int y2=rand.Next (image.Height);   g。画直线(linePen (x1, y1, x2, y2);   }//随机点   for (int i=0;我& lt;30;我+ +)   {   int x=rand.Next (image.Width);   int y=rand.Next (image.Height);   的形象。SetPixel (x, y, Color.Gray);   }//边框   g。DrawRectangle(新钢笔(Color.Gray), 0, 0,形象。宽度- 1,形象。高度- 1);//输出图片   先。MemoryStream=new System.IO.MemoryStream女士();   的形象。保存(女士,System.Drawing.Imaging.ImageFormat.Jpeg);   context.Response.ClearContent ();   context.Response。ContentType=巴枷?jpeg";   context.Response.BinaryWrite (ms.ToArray ());   g.Dispose ();   image.Dispose ();   }   }

封装成类库:ValidateNumber。cs

使用系统;
  使用System.Collections.Generic;
  使用来;
  使用包含;
  使用System.Drawing;
  使用System.Web.UI;
  使用System.Drawing.Drawing2D;
  使用先;
  使用System.Drawing.Imaging;///& lt; summary>///ValidateNumber生成验证码///& lt;/summary>
  公开课ValidateNumber
  {//产生验证码的字符集(易混淆的字符去掉)
  私人字符串charcode=?, 3, 4, 5, 6, 8, 9, A, B, C, D, E, F, G, H, J, K、M, N, P, R, S, U, W, X, Y, A, B, C, D, E, F, G, H, J, K、M, N, P, R, S, U, W, X, y";///& lt; summary>///验证码的最大长度///& lt;/summary>
  公共int最大长度
  {
  {回报10;}
  }///& lt; summary>///验证码的最小长度///& lt;/summary>
  公共int最小长度
  {
  得到{返回1;}
  }///& lt; summary>///生成验证码///& lt;/summary>///& lt;参数name=發ength"在指定验证码的长度& lt;/param>///& lt; returns> & lt;/returns>
  公共字符串CreateValidateNumber (int长度)
  {
  string [] CharArray=charcode.Split(& # 39; & # 39;);//将字符串转换为字符数组
  字符串randomCode=?“;
  int temp=1;
  随机兰德=new随机();
  for (int i=0;我& lt;长度;我+ +)
  {
  如果(临时!=1)
  {
  兰德=new随机(我*临时* ((int) DateTime.Now.Ticks));
  }
  int t=rand.Next (CharArray。长度- 1);
  如果(临时!=1,,temp==t)
  {
  返回CreateValidateNumber(长度);
  }
  temp=t;
  randomCode +=CharArray [t];
  }
  返回randomCode;
  }///& lt; summary>///创建验证码的图片///& lt;/summary>///& lt;参数name=癱ontext"在上下文对象& lt;/param>///& lt;参数name=皏alidateNum"在验证码& lt;/param>
  公共空间CreateValidateGraphic (HttpContext上下文字符串validateNum)
  {
  int iwidth=(int) (validateNum。长度* 14);
  位图图像=新的位图(iwidth, 22);
  图g=Graphics.FromImage(图片);
  试一试
  {//生成随机生成器
  随机随机=new随机();//清空图片背景色
  g.Clear (Color.White);//画图片的干扰线
  for (int i=0;我& lt;25;我+ +)
  {
  int x1=random.Next (image.Width);
  int x2=random.Next (image.Width);
  int y?=random.Next (image.Height);
  int y2=random.Next (image.Height);
  g。画直线(新钢笔(Color.Silver) (x1, y1, x2, y2);
  }
  字体字体=new字体(“Arial" 12 (FontStyle。大胆| FontStyle.Italic));
  LinearGradientBrush刷=new LinearGradientBrush(新矩形(0,0,形象。宽度,image.Height),
  颜色。蓝色,颜色。DarkRed 1.2 f,真实);
  g。拉带(validateNum、字体、画笔、3、2);//画图片的前景干扰点
  for (int i=0;我& lt;100;我+ +)
  {
  int x=random.Next (image.Width);
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null
  null

怎么使用ASP.NET实现生成验证码功能