ASP. NET Verification Code (3 types) and asp.net Verification Code (3 types)
In daily life, we encounter verification codes when using websites. Have you ever wondered why you should use the verification codes?
In fact, the purpose of the Verification Code is to prevent malicious password cracking, ticket refreshing, Forum bumping, and page refreshing. This effectively prevents a hacker from continuously logging on to a specific registered user using brute force cracking of a specific program. Today, I will share with you three verification codes of ASP. NET.
1. GSC_WebControlLibrary this is a control found on the Internet and is very useful. But the effect is not very good (see.
) Although it is easy to use, all attributes can be set like controls, but the availability is not high. The user cannot customize the verification code, and it seems that the verification code is not very effective.
Effect:
2. Use one page to generate images, call the other page, and store the verification codeCookieThe cookie is used for comparison and verification during the call. This user can change the effect and verification code length according to their preferences.
Effect
The CheckCode. aspx code is as follows:
Using System; using System. data; using System. configuration; using System. collections; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. drawing; using System. drawing. drawing2D; using System. drawing. imaging; public partial class Tools_CheckCode: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {this. createCheckCodeImage (GenerateCheckCode ();} private string GenerateCheckCode () {int number; char code; string checkCode = String. empty; System. random random = new Random (); for (int I = 0; I <5; I ++) {number = random. next (); if (number % 2 = 0) code = (char) ('0' + (char) (number % 10); else code = (char) ('A' + (char) (number % 26); checkCode + = code. toString ();} Response. cookies. add (new HttpCookie ("CheckCode", checkCode); return checkCode;} private void CreateCheckCodeImage (string checkCode) {if (checkCode = null | checkCode. trim () = String. empty) return; System. drawing. bitmap image = new System. drawing. bitmap (int) Math. ceiling (checkCode. length * 12.5), 22); Graphics g = Graphics. fromImage (image); try {// generate Random generator random Random = new Random (); // clear the image background color g. clear (Color. white); // specifies the background noise line of the image. for (int I = 0; I <25; I ++) {int x1 = random. next (image. width); int x2 = random. next (image. width); int y1 = random. next (image. height); int y2 = random. next (image. height); g. drawLine (new Pen (Color. greenYellow), x1, y1, x2, y2);} Font font = new System. drawing. font ("Verdana", 12, (System. drawing. fontStyle. bold | System. drawing. fontStyle. italic); System. drawing. drawing2D. linearGradientBrush brush = new System. drawing. drawing2D. linearGradientBrush (new Rectangle (0, 0, image. width, image. height), Color. blue, Color. darkRed, 1.2f, true); g. drawString (checkCode, font, brush, 2, 2); // foreground noise of the image to be painted for (int I = 0; I <80; I ++) {int x = random. next (image. width); int y = random. next (image. height); image. setPixel (x, y, Color. fromArgb (random. next ();} // draw the border line g of the image. drawRectangle (new Pen (Color. red), 0, 0, image. width-1, image. height-1); System. IO. memoryStream MS = new System. IO. memoryStream (); image. save (MS, System. drawing. imaging. imageFormat. gif); Response. clearContent (); Response. contentType = "image/Gif"; Response. binaryWrite (ms. toArray ();} finally {g. dispose (); image. dispose ();}}}
Then, reference on the page to be used:
UseCheckCode. aspx
3. Use web handler to generate images. In fact, this is roughly the same as the previous meaning, and the calling method is basically the same as 2. The difference is that its verification code is savedSession. For Learning reference.
As follows:
ValidateImageHandler. ashx
% @ WebHandler Language = "C #" Class = "ValidateImageHandler" %> using System; using System. web; using System. web. sessionState; using System. drawing; using System. drawing. imaging; using System. text; // <summary> /// ValidateImageHandler the website verification code generation function // </summary> public class ValidateImageHandler: IHttpHandler, IRequiresSessionState {int intLength = 5; // string strIdentify = "Identify"; // a random string stores the key value for storing it in the Session public ValidateImageHandler () {}/// <summary> /// generate the verification image core code /// </summary> /// <param name = "hc"> </param> public void processRequest (HttpContext hc) {// set the output stream image format hc. response. contentType = "image/gif"; Bitmap B = new Bitmap (200, 60); Graphics g = Graphics. fromImage (B); g. fillRectangle (new SolidBrush (Color. yellowGreen), 0, 0,200, 60); Font font = new Font (FontFamily. genericSerif, 48, FontStyle. bold, GraphicsUnit. pixel); Random r = new Random (); // valid Random display character list string strLetters = "character"; StringBuilder s = new StringBuilder (); // draw the randomly generated string to the image for (int I = 0; I <intLength; I ++) {s. append (strLetters. substring (r. next (0, strLetters. length-1), 1); g. drawString (s [s. length-1]. toString (), font, new SolidBrush (Color. blue), I * 38, r. next (0, 15);} // generate interference line Pen pen = new Pen (new SolidBrush (Color. blue), 2); for (int I = 0; I <10; I ++) {g. drawLine (pen, new Point (r. next (1, 0,199), r. next (0, 59), new Point (r. next (1, 0,199), r. next (0, 59);} B. save (hc. response. outputStream, ImageFormat. gif); hc. session [strIdentify] = s. toString (); // It is saved in the Session and verified to be consistent with user input. hc. response. end () ;}/// <summary> /// indicates whether the instance can be shared by multiple requests (reuse can improve performance) /// </summary> public bool IsReusable {get {return true ;}}}
The above three types of verification codes are ASP. net. they have their own advantages and disadvantages. I hope you can implement the functions of different verification codes.