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 ();
}
}
From Xu Yue's column