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
Code:
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 (pens.red,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 (color.red) $,NewPointF (R.next ( the), R.next (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 =r.next (width); $ inty =r.next (height); the the G.drawline (Pens.blue 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[r.next (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.