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.