当前位置:编程学习 > asp >>

ASP.NET生成验证码

生成验证码原理:产生随机字符,并将字符生成为图片,同时储存到Session里去,然后验证用户输入的内容是否与Session中的验证码相符即可。
 
效果图:用户可以点击切换验证码信息。
 \
 
一般处理程序:CheckCodeHandler.cs
 
  1 <%@ WebHandler Language="C#" Class="CheckCodeHandler" %>
  2
  3 using System;
  4 using System.Web;
  5 using System.Text;
  6 using System.Drawing;
  7 using System.Web.SessionState;
  8
  9 public class CheckCodeHandler : IHttpHandler,IRequiresSessionState
 10 {
 11
 12     //产生验证码的字符集
 13     public string charcode = "2,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";
 14    
 15     public void ProcessRequest (HttpContext context) {
 16         string validateCode = CreateRandomCode(4);
 17         context.Session["ValidateCode"] = validateCode;//将验证码保存到session中
 18         CreateCodeImage(validateCode, context);
 19     }
 20 
 21     public bool IsReusable {
 22         get {
 23             return false;
 24         }
 25     }
 26
 27     /// <summary>
 28     /// 生成验证码
 29     /// </summary>
 30     /// <param name="n">验证码个数</param>
 31     /// <returns>验证码字符串</returns>
 32     public string CreateRandomCode(int n)
 33     {
 34         string[] CharArray = charcode.Split(',');//将字符串转换为字符数组
 35         string randomCode = "";
 36         int temp = -1;
 37
 38         Random rand = new Random();
 39         for (int i = 0; i < n; i++)
 40         {
 41             if (temp != -1)
 42             {
 43                 rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
 44             }
 45             int t = rand.Next(CharArray.Length - 1);
 46             if (temp != -1 && temp == t)
 47             {
 48                 return CreateRandomCode(n);
 49             }
 50             temp = t;
 51             randomCode += CharArray[t];
 52         }
 53         return randomCode;
 54     }
 55
 56     public void CreateCodeImage(string checkCode, HttpContext context)
 57     {
 58         int iwidth = (int)(checkCode.Length * 13);
 59         System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
 60         Graphics g = Graphics.FromImage(image);
 61         Font f = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold));
 62
 63         // 前景色
 64         Brush b = new System.Drawing.SolidBrush(Color.Black);
 65
 66         // 背景色
 67         g.Clear(Color.White);
 68
 69         // 填充文字
 70         g.DrawString(checkCode, f, b, 0, 1);
 71
 72         // 随机线条
 73        
 74         Pen linePen = new Pen(Color.Gray, 0);
 75         Random rand = new Random();
 76    
 77         for (int i = 0; i < 5; i++)
 78         {
 79             int x1 = rand.Next(image.Width);
 80             int y1 = rand.Next(image.Height);
 81             int x2 = rand.Next(image.Width);
 82             int y2 = rand.Next(image.Height);
 83             g.DrawLine(linePen, x1, y1, x2, y2);
 84         }
 85       
 86         // 随机点
 87         for (int i = 0; i < 30; i++)
 88         {
 89             int x = rand.Next(image.Width);
 90             int y = rand.Next(image.Height);
 91             image.SetPixel(x, y, Color.Gray);
 92         }
 93
 94         // 边框
 95         g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1);
 96
 97         // 输出图片
 98         System.IO.MemoryStream ms = new System.IO.MemoryStream();
&nb

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,