About implementing basic verification codes in general processing programs

Source: Internet
Author: User

A verification code is dynamically generated for an image. The common verification code is stored on the server.

To use session in a general processing program, the System. Web. SessionState. IRequiresSessionState interface must be implemented. Otherwise, the session cannot be found.

Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "image/JPEG"; // The format to be corrected is (text/plan)
// Create a bitmap to determine the image size (image size)
Using (System. Drawing. Bitmap bitmip = new System. Drawing. Bitmap (100,50 ))
{// Create a canvas to draw the image you just created
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmip ))
{
// Create an int-type verification code.
Random rand = new Random ();
Int code = rand. Next ();
String strcode = code. ToString ();
// Save the generated verification code to the session
HttpContext. Current. Session ["code"] = strcode;
// Set the content, font size, and font color of the canvas.
G. drawString (strcode, new System. drawing. font ("", 12), System. drawing. brushes. yellow, new System. drawing. pointF (0, 0); // The font is to be recycled.
// Save
Bitmip. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
}

The value of the session called on the aspx page is compared with that submitted by the user.

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.