Generate a verification code using a generic handler in ASP.

Source: Internet
Author: User

If you expect the generic handler (ASHX) to handle the session, you must implement the "IRequiresSessionState" interface under the "System.Web.SessionState" namespace.

The verification code in ASP. NET is usually done by requesting the Ashx page, and the response type of the ASHX context object is set to "Image/jpeg". Using the random class is randomly generating a string, writing the string to a picture and saving it to the session, Then save the picture to the output of the response stream so that each time the client requests a picture of the verification code.

Here are my specific implementations:

Client

The verification code image of the SRC attribute point to the path of the ASHX Verification Code processing program, click on the picture or "can't see" realization of the function of the image change by changing the SRC attribute of the verification code picture. Each time the verification code is replaced, in order to avoid the cache without making a request, you need to add an invariant parameter at the end of the URL of the SRC attribute, such as: this.src= "getvalidatecode.ashx?t=" +new Date (); Because the browser takes precedence from the cache when it processes the same request, the browser sends a request for each request, rather than reading from the cache.


Server-side

Method of generating random code:

1 Private stringGetvalidatecode (intvalidatecodelength)2 {3     // define all the possible strings, some characters in the actual application are difficult to distinguish, such as 0 and O, can be removed. 4     stringAllchars =@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";5     // get the length of all characters6     intCharslength =allchars.length;7     8     stringValidatecode =string. Empty;9     // declaring a random number generatorTenRandom ran =NewRandom (); One     //The number of cycles represents the length of the generated random string A      for(inti =0; i < validatecodelength; i++) -     { -       //The randomly generated number as the index of the string, so that it can get the character of its subscript, and multibyte the word to a random string theValidatecode + =Allchars[ran. Next (Charslength)]; -     } -     //return String -     returnValidatecode; +}


Ashx in the process of processing

1  Public voidProcessRequest (HttpContext context)2 {3     //set Response type to picture4Context. Response.ContentType ="Image/jpeg";5     //get a 4-digit verification Code6     stringValidatecode = Getvalidatecode (4);7     //write verification code to session8Context. session["Validatecode"] =Validatecode;9     //Create a Captcha pictureTen     using(Bitmap img =NewBitmap ( -, +)) One     { A       //Get background picture path for background verification code -       stringBgpath = context. Server.MapPath ("~/image/bg.gif"); -       using(Bitmap bg =NewBitmap (bgpath)) the       { -         using(Graphics g =graphics.fromimage (img)) -         { -           //fill the captcha picture with a background picture paint brush +G.fillrectangle (NewTextureBrush (BG),0,0, IMG. Width, IMG. Height); -           //write a CAPTCHA string to a picture +g.DrawString (Validatecode,NewFont ("Microsoft Ya-Black", -, Fontstyle.italic), Brushes.black,5,5); A           //Save the Captcha picture to the response stream and the client will see the image. at           img. Save (context. Response.outputstream, imageformat.jpeg); -         } -       } -     } -}

Generate a verification code using a generic handler in ASP.

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.