.net 项目学习总结 day01 验证码实现

第一步,花验证码   纯数字验证码,包含干扰点,无线

 1  /// <summary>
 2     /// ValidateCode 的摘要说明
 3     /// </summary>
 4     public class ValidateCode : IHttpHandler,IRequiresSessionState
 5     {
 6         HttpContext context;//新建
 7         public void ProcessRequest(HttpContext context1)
 8         {
 9             this.context = context1;//一个获取 一个新建   实现刷新
10             CreateCheckCodeImage(GenreateCheckCode());//生成验证码
11         }
12         /// <summary>
13         /// 生成验证码文本
14         /// </summary>
15         /// <returns></returns>
16         private string GenreateCheckCode()
17         {
18             int number;
19             char code;
20             string checkCode = string.Empty;
21 
22             System.Random random = new Random();
23             for (int i = 0; i < 5; i++)
24             {
25                 number = random.Next();
26                 if(number%2==0)
27                     code=(char)('0'+(char)(number%10));
28                 else
                        code=(char)('0'+(char)(number%10));
            //code = (char)('A' + (char)(number % 26));  
30                 checkCode += code.ToString();
31             }
32             context.Session.Add("vCode", checkCode);
33             return checkCode;
34         }
35 
36         private void CreateCheckCodeImage(string checkCode)
37         {
38             if (checkCode == null || checkCode.Trim() == string.Empty)
39                 return;
40 
41             System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
42             Graphics g = Graphics.FromImage(image);
43 
44             try
45             {
46                 //生成随机生成器
47                 Random random = new Random();
48                 //清空图片背景
49                 g.Clear(Color.White);
50                 //画图片的背景噪音线
51                 for (int i = 0; i < 25; i++)
52                 {
53                     int x1 = random.Next(image.Width);
54                     int x2 = random.Next(image.Width);
55                     int y1 = random.Next(image.Height);
56                     int y2 = random.Next(image.Height);
57 
58                     g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
59 
60                 }
61                 Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold |
62                     System.Drawing.FontStyle.Italic));
63                 System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(
64                     new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
65                 g.DrawString(checkCode, font, brush, 2, 2);
66                 //画图片的前景噪音点
67                 for (int i = 0; i < 100; i++)
68                 {
69                     int x = random.Next(image.Width);
70                     int y = random.Next(image.Height);
71 
72                     image.SetPixel(x, y, Color.FromArgb(random.Next()));
73 
74 
75                 }
76                 //画图片的边框线
77                 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
78 
79                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
80                 image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
81                 context.Response.ClearContent();
82                 context.Response.ContentType = "image/Gif";
83                 context.Response.BinaryWrite(ms.ToArray());
84             }
85             finally
86             {
87                 g.Dispose();
88                 image.Dispose();
89             }
90         }
91         public bool IsReusable
92         {
93             get
94             {
95                 return false;
96             }
97         }
98     }
   第二步  验证码校验
 1     /// <summary>
 2       /// 完成验证码的校验
 3       /// </summary>
 4       /// <returns></returns>
 5       public static bool CheckValidateCode()
 6       {
 7           HttpContext context = HttpContext.Current;
 8           bool isSucess = false;
 9           if (context.Session["vCode"] != null)
10           {
11               string code = context.Request["txtCode"];//文本框输入的值
12               string sysCode = context.Session["vCode"].ToString();
13     //忽略大小写
14               if (sysCode.Equals(code, StringComparison.InvariantCultureIgnoreCase))
15               {
16                   //context.Session["vCode"] = null;
17                   isSucess = true;
18               }
19           }
20           return isSucess;
21       }