Learn more about. NET verification code generation and how to use _ Practical skills

Source: Internet
Author: User
Tags datetime

Small class: The role of verification Code:

A few years ago, most of the sites, forums and so on 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 in endlessly, greatly increasing the burden of the site 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 recognize, the program is not easy to identify the code!

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

However, with the development of computer science, pattern recognition and other technologies become more and more mature, so the guy who wrote the robot program will be directly written in the picture of the content identified, and then submitted to the server, so that the verification code will be a fake. In order to prevent the robot program recognition, the verification code picture generation also unceasingly in the development, joins the disturbing point, the interference line, the text distortion, transforms the angle position, the color is different . Various techniques to prevent computer recognition are also applied to the verification code. In the competition between the two technologies, so that we now see the verification code, there have been a lot of people complaining, "what is this verification code oh, the human eye can not tell what is not clear," everything is helpless.

Understand the role of the verification code, below write a simple verification code generation and use of the example

First create a page to show the verification code and to determine whether the validation code input is correct

 
 

Verify code is validated in the background of this page

 protected void Page_Load (object sender, EventArgs e)
 {
 //generated verification code saved to session
 if (session["Checkcode"]!= NULL)
 {
 string checkcode = session["Checkcode"]. ToString ();
 if (this. TextBox1.Text = = Checkcode)
 {
  Clientscript.registerclientscriptblock (this. GetType (), "", "Alert (' Authentication code input is correct! ')", true)
 ;
 else
 {
  Clientscript.registerclientscriptblock (this. GetType (), "", "Alert (' Authentication code input Error! ')", true);
 }

 

Generate Authentication 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 (10); 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) re
 Turn
 int iwordwidth = 15;
 int iimagewidth = Checkcode.length * iwordwidth;
 Bitmap image = New Bitmap (iimagewidth, 20);
 Graphics g = graphics.fromimage (image);
 try {//Generate a random generator Random Random = new Random ();

 Clear the picture background color g.clear (color.white); The background noise point for the picture is 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 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 &lT 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," (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 = new System.Drawing.Font (bold), (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's 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 picture's border line 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 ();
 }
 }

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.