Asp. NET in the actual development of the use of verification code

Source: Internet
Author: User

There are many codes on the Internet to see the code, many are just to generate a verification code picture, however, in the actual login verification module, how to add the verification code or how to use, and the actual project development in how to using the verification code, I summed up a few points.

First, in the actual development of the login module verification code, the programmer is the verification Code text value (string) exists in the session, and then in the login verification, through the session to determine the value, so the efficiency will be much higher.

Second, while writing the verification code to pass the session value, it is necessary to implement the System.Web.SessionState.IRequiresSessionState interface

Third, the general Processing procedures (Ashx page) for the column, the following to the code to explain the wording and use of detailed


1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Drawing;4 usingSystem.Linq;5 usingsystem.web;6 7 namespaceVcodedemo8 {9     /// <summary>Ten     ///Vcode Description of the wording One     /// </summary> A      Public classc01vcode:ihttphandler,system.web.sessionstate.irequiressessionstate -     //If you want to use the session normally in a generic handler, you must implement the IRequiresSessionState interface -     { the          Public voidProcessRequest (HttpContext context) -         { -             //1 Setting contenttype as the picture type -Context. Response.ContentType ="Image/jpeg"; +  -             //2 Preparing the Picture object to be painted, width 80 height, Bitmap: Bitmap +             using(Image img =NewBitmap ( the, -)) A             { at                 //defining an artist from an IMG object -                 using(Graphics g =graphics.fromimage (img)) -                 { -                     //clear the background of the bitmap with white - g.clear (color.white); -  in                     //the border of the picture is red, and the entire picture is drawn from the top left corner . -G.drawrectangle (,0,0Img. Width-1Img. Height-1); to  +                     //Draw 50 noise points in front of the captcha text -                      This. Drawpoint ( -, G, IMG. Width, IMG. Height); the  *                     //Get the Captcha text string (randomly 4 characters) $                     stringVcode = This. Getvcode (4);Panax Notoginseng  -                     //Save the Captcha text string to the session theContext. session["Vcode"] =Vcode; +  A                     //to write a captcha string to a Picture object the g.drawstring (Vcode +,NewFont ("Arial", -, Fontstyle.strikeout | FontStyle.Bold)//add a horizontal line and bold to text -,NewSolidBrush ( $,NewPointF ( ( the), (8)) $                         ); -  -                     //Draw 50 Noise behind the captcha text the                      This. Drawpoint ( -, G, IMG. Width, IMG. Height); -                 }Wuyi                 //output Verification code to the browser the img. Save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg); -             } Wu         } -  About         /// <summary> $         ///draw noise on a picture object -         /// </summary> -         /// <param name= "Count" ></param> -         voidDrawpoint (intCount, Graphics G,intWidthintheight) A         { +              for(inti =0; I < count; i++) the             { -                 intx (width); $                 inty (height); the  the G.drawline ( the,NewPoint (x, y) the,NewPoint (x +2, Y +2) -                     ); in             } the         } the  About         /// <summary> the         ///define an object that produces a random number the         /// </summary> theRandom r =NewRandom (); +  -         /// <summary> the         ///generate Authenticode text stringBayi         /// </summary> the         /// <param name= "Count" ></param> the         /// <returns></returns> -         stringGetvcode (intcount) -         { the             //declaring a return value the             stringRescode =""; the             stringCodestr ="ABCDabcd123456789"; the             Char[] Codearr =Codestr. ToArray (); -              for(inti =0; I < count; i++) the             { theRescode + =Codearr[ (codestr. Length)]; the             }94             //return String the             returnRescode; the         } the 98          Public BOOLisreusable About         { -             Get101             {102                 return false;103             }104         } the     }106}

When verifying the landing judgment, we assign the CAPTCHA text to the session through the session of the context object: Context. session["Vcode"] = Vcode, all of which can be evaluated using session["Vcode" at the time of validation and then judged.

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: 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.