ASP. NET Verification Code Generation

Source: Internet
Author: User

Verification Code Generation Principle: generate a random character, generate the character into an image, and store it in the Session. Then, verify that the content entered by the user matches the verification code in the Session.
 
: You can click to switch the verification code information.
 
 
General handler: 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 // character set that generates the verification code
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; // Save the verification code to the session.
18 CreateCodeImage (validateCode, context );
19}
20
21 public bool IsReusable {
22 get {
23 return false;
24}
25}
26
27 /// <summary>
28 // generate the verification code
29 /// </summary>
30 /// <param name = "n"> Number of verification codes </param>
31 /// <returns> Verification code string </returns>
32 public string CreateRandomCode (int n)
33 {
34 string [] CharArray = charcode. Split (','); // convert a string to a character array
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 // foreground
64 Brush B = new System. Drawing. SolidBrush (Color. Black );
65
66 // background color
67g. Clear (Color. White );
68
69 // fill in text
70g. DrawString (checkCode, f, B, 0, 1 );
71
72 // random line
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 );
83g. DrawLine (linePen, x1, y1, x2, y2 );
84}
85
86 // random point
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 // border
95g. DrawRectangle (new Pen (Color. Gray), 0, 0, image. Width-1, image. Height-1 );
96
97 // output image
98 System. IO. MemoryStream MS = new System. IO. MemoryStream ();
99 image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
100 context. Response. ClearContent ();
101 context. Response. ContentType = "image/jpeg ";
102 context. Response. BinaryWrite (ms. ToArray ());
103g. Dispose ();
104 image. Dispose ();
105}
106}
 
 
Encapsulated as a Class Library: ValidateNumber. cs
 
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Web;
5 using System. Drawing;
6 using System. Web. UI;
7 using System. Drawing. Drawing2D;
8 using System. IO;
9 using System. Drawing. Imaging;
10
11 /// <summary>
12 // ValidateNumber
13 /// </summary>
14 public class ValidateNumber
15 {
16 // character set that generates the verification code (confusing characters are removed)
17 private 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 ";
18
19 /// <summary>
20 /// Maximum length of the Verification Code
21 /// </summary>
22 public int MaxLength
23 {
24 get {return 10 ;}
25}
26
27 /// <summary>
28 /// minimum length of the Verification Code
29 /// </summary>
30 public int MinLength
31 {
32 get {return 1 ;}
33}
34
35 /// <summary>
36 // generate the verification code
37 /// </summary>
38 // <param name = "length"> specify the length of the Verification Code </param>
39 /// <returns> </returns>
40 public string CreateValidateNumber (int length)
41 {
42 string [] CharArray = charcode. Split (','); // convert a string to a character array
43 string randomCode = "";
44 int temp =-1;
45
46 Random rand = new Random ();
47 for (int I = 0; I <length; I ++)
48 {
49 if (temp! =-1)
50 {
51 rand = new Random (I * temp * (int) DateTime. Now. Ticks ));
52}
53 int t = rand. Next (CharArray. Length-1 );
54 if (temp! =-1 & temp = t)
55 {
56 return CreateValidateNumber (length );
57}
58 temp = t;
59 randomCode + = CharArray [t];
60}
61 return randomCode;
62}
63
64 /// <summary>
65 // create a verification code Image
66 /// </summary>
67 // <param name = "context"> context object </param>
68 // <param name = "validateNum"> Verification Code </param>
69 public void CreateValidateGraphic (HttpContext context, string validateNum)
70 {
71 int iwidth = (int) (validateNum. Length * 14 );
72 Bitmap image = new Bitmap (iwidth, 22 );
73 Graphics g = Graphics. FromImage (image );
74 try
75 {
76 // generate a random Generator
77 Random random = new Random ();
78 // clear the background color of the image
79g. Clear (Color. White );
80 // picture interference line
81 for (int I = 0; I <25; I ++)
82 {
83 int x1 = random. Next (image. Width );
84 int x2 = random. Next (image. Width );
85 int y1 = random. Next (image. Height );
86 int y2 = random. Next (image. Height );
87g. DrawLine (new Pen (Color. Silver), x1, y1, x2, y2 );
88}
89 Font font = new Font ("Arial", 12, (FontStyle. Bold | FontStyle. Italic ));
90 LinearGradientBrush brush = new LinearGradientBrush (new Rectangle (0, 0, image. Width, image. Height ),
91 Color. Blue, Color. DarkRed, 1.2f, true );
92g. DrawString (validateNum, font, brush, 3, 2 );
93 // foreground interference points
94 for (int I = 0; I <100; I ++)
95 {
96 int x = random. Next (image. Width );
97 int y = random. Next (image. Height );
98 image. SetPixel (x, y, Color. FromArgb (random. Next ()));
99}
100 // draw the border line of the image
101g. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
102 // Save image data
103 MemoryStream stream = new MemoryStream ();
104 image. Save (stream, ImageFormat. Jpeg );
105 // output image
106 context. Response. Clear ();
107 context. Response. ContentType = "image/jpeg ";
108 context. Response. BinaryWrite (stream. ToArray ());
109}
110 finally
111 {
112g. Dispose ();
113 image. Dispose ();
114}
115}
116 /// <summary>
117 // obtain the length of the Verification Code Image
118 /// </summary>
119 /// <param name = "validateNumLength"> Verification code length </param>
120 /// <returns> </returns>
121 public static int GetImageWidth (int validateNumLength)
122 {
123 return (int) (validateNumLength * 14 );
124}
125 /// <summary>
126 // obtain the height of the Verification Code Image
127 /// </summary>
128 /// <returns> </returns>
129 public static double GetImageHeight ()
130 {
131 return 22;
132}
133}
 
 
Example download: http://www.bkjia.com/uploadfile/2012/0528/20120528090736627.rar
 

 

 

Author: ForEvErNoME

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.