How to generate and use the. NET Verification Code

Source: Internet
Author: User
Small classroom: The role of verification Code:

A few years ago, most of the sites, forums and the like are no verification code, because for the average user, the verification code only increases the user's operation, reducing the user's experience. But then a variety of irrigation robots, voting robots, malicious registered robots emerge, greatly increased the burden of the site and also to the site database brought a lot of garbage data. In order to prevent the destruction of various robot programs, so the programmer came up with only the human eye can identify, the program is not easy to identify the verification code!

Verification code is a picture, the letter, numbers and even Chinese characters as the content of the picture, such a picture of the content of the human eye is easy to identify, and the program will not be recognized. Before the database operation (such as login verification, voting, Post, reply, registration, etc.) the program first verifies that the client submitted the same verification code as the content in the picture, if the same database operation, the difference is that the code is wrong, do not perform database operations. So all kinds of robot programs will be shut down!

But with the development of computer science, pattern recognition and other techniques more and more mature, so the guy who wrote the robot program can be directly written in the image of the content to identify, and then submitted to the server, so that the verification code will be the same as a dummy. In order to prevent the identification of the robot program, the image generation of verification code is constantly developing, adding interference points, interference lines, text deformation, change angle position, different colors ... Various techniques to prevent computer recognition are also applied to the verification code. In the two technology competition, so that we now see the verification code, there are many people complaining "what is this verification code oh, the human eye is not clear what is", all is helpless.

Understand the role of verification code, the following is a simple verification code generation and use of the example

First to create a page to show the verification code and verify that the code input is correct


Verify the verification code in the background of this page

protected void Page_Load (object sender, EventArgs e) {//Generated verification code is saved to Session if (session["Checkcode"]! = null) {string Checkcode = session["Checkcode"]. ToString (); if (this. TextBox1.Text = = Checkcode) {Clientscript.registerclientscriptblock (this. GetType (), "", "alert (' verify code entered correctly! ')", true);} else{Clientscript.registerclientscriptblock (this. GetType (), "", "alert (' CAPTCHA input Error! ')", True);}}

Generate Verification Code page png.aspx

protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {createcheckcodeimage (Generatecheckcodes (4));}} public void Showauthcode (stream stream, out string code) {Random random = new random (); code = random. Next (1000, 9999). ToString (); Bitmap Bitmap = createauthcode (code); Bitmap. Save (stream, System.Drawing.Imaging.ImageFormat.Gif);} private string generatecheckcodes (int iCount) {int number;string Checkcode = String.empty;int ISeed = DateTime.Now.Millisecond; System.Random random = new Random (iSeed), for (int i = 0; i < ICount; i++) {number = Random. Next (+); Checkcode + = number. ToString ();} session["Checkcode"] = Checkcode;return checkcode;} Private Bitmap Createauthcode (String str) {Font fn = new Font ("Arial", 12); Brush ForeColor = Brushes.black; Brush bgcolor = brushes.white; PointF PF = new PointF (5, 5); Bitmap Bitmap = new Bitmap (100, 25); Rectangle rec = new Rectangle (0, 0, 100, 25); Graphics gh = graphics.fromimage (bitmap); Gh. FillRectangle (bgcolor, rec); Gh. DrawString (str, FN, ForecoLor, pf); return bitmap;} private void Createcheckcodeimage (string checkcode) {if (Checkcode = = NULL | | Checkcode.trim () = = String.Empty) return;int Iwordwidth = 15;int Iimagewidth = checkcode.length * iwordwidth; Bitmap image = New Bitmap (iimagewidth, 20); Graphics g = graphics.fromimage (image); try{//generates random generator randomly random = new random ();//Empty picture background color g.clear (color.white); Draw the background noise point for the picture for (int i = 0; i <; 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);} Picture the background noise line for (int i = 0; i < 2; i++) {int x1 = 0; int x2 = image. Width; int y1 = random. Next (image. Height); int y2 = random. Next (image. Height);  if (i = = 0) {g.drawline (new Pen (Color.gray, 2), x1, y1, x2, y2);}} for (int i = 0; i < checkcode.length; i++) {string Code = Checkcode[i]. ToString (); int xleft = Iwordwidth * (i); Random = new Random (xleft); int iSeed = DateTime.Now.MillIsecond; int ivalue = random. Next (iSeed)% 4; if (Ivalue = = 0) {font font = new Font ("Arial", +, (FontStyle.Bold | System.Drawing.FontStyle.Italic)); Rectangle rc = new Rectangle (xleft, 0, iwordwidth, image. Height); LinearGradientBrush brush = new LinearGradientBrush (RC, Color.Blue, Color.Red, 1.5f, true); g.DrawString (Code, font, Brush, xleft, 2); } else if (ivalue = = 1) {font font = new System.Drawing.Font ("italics", (FontStyle.Bold)); Rectangle rc = new Rectangle (xleft, 0, iwordwidth, image. Height); LinearGradientBrush brush = new LinearGradientBrush (RC, Color.Blue, color.darkred, 1.3f, true); g.DrawString (Code, font, Brush, xleft, 2); } else if (Ivalue = = 2) {font font = new System.Drawing.Font ("Arial", and (System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle (xleft, 0, iwordwidth, image. Height); LinearGradientBrush brush = new LinearGradientBrush (RC, Color.green, Color.Blue, 1.2f, true); g.DrawString (Code, font, Brush, xleft, 2); } else if (Ivalue = = 3) {Font font = newSystem.Drawing.Font ("blackbody", +, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle (xleft, 0, iwordwidth, image. Height); LinearGradientBrush brush = new LinearGradientBrush (RC, Color.Blue, Color.green, 1.8f, true); g.DrawString (Code, font, Brush, xleft, 2); }}//////picture of the foreground noise point//for (int i = 0; i < 8; i++)//{//int x = random. Next (image. Width);//int y = random. Next (image. Height);//image. SetPixel (x, Y, Color.FromArgb (random. Next ()));//}//draw the border line of the picture 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); Response.clearcontent (); Response.BinaryWrite (Ms. ToArray ());} Finally{g.dispose (); image. Dispose ();}}
Related Article

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.