Register. aspx
// When you click the verification code image, the authcode. aspx is automatically redirected and the verification code is refreshed again.
$ ('# Authimage'). Click (function (){
$ (This). ATTR ("src", "authcode. aspx ");
});
Verification Code: <input id = "authcode" type = "text" class = "required" name = "authcode"/>
Note: the path of the Verification Code image is a dynamic page! We use the GDI + technology to draw verification codes on this dynamic page.
Authcode. CS
Public partial class authcode: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// Possible characters in the verification code
String authcodestring = "0123456789 abcdefghijklmnopqrstuvwxyz ";
// Character set length of the Verification Code
Int length = authcodestring. length;
// Draw Character Font
Font F = new font ("", 24, fontstyle. Bold );
// Specifies the image painter who draws the verification code.
Brush B = NULL;
// Color of the Verification Code
Color brushcolor = new color ();
Bitmap image = new Bitmap (80, 40 );
Graphics G = graphics. fromimage (image );
G. Clear (color. Gray); // sets the background.
String authcode = string. Empty; // The verification code displayed to the user
String code = string. Empty; // The currently drawn Verification Code
Random random = new random ();
For (INT I = 0; I <4; I ++)
{
// The remainder ensures that the current length does not exceed the combined length of the Verification Code Character Set
Int current = random. Next (datetime. Now. millisecond) % length );
// The Character Set of the verification code can be used to intercept any character.
Code = authcodestring. substring (current, 1 );
Authcode + = code;
Brushcolor = color. fromargb (random. Next (255), random. Next (255), random. Next (255 ));
B = new solidbrush (brushcolor );
// Draw the string you just obtained
G. drawstring (Code, F, B, I * 15, 2 );
}
Response. Clear ();
Response. contenttype = "image/pjpeg ";
// Save the object to the response output stream
Image. Save (response. outputstream, imageformat. JPEG );
Image. Dispose ();
Session ["authcode"] = authcode; // Save the verification code on the server side for comparison
Response. End ();
}
}