ASP. NET Verification Code (3 types)

Source: Internet
Author: User
ASP. NET Verification Code (3 types)
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 an image, and the other page to call it. The verification code is saved to the cookie, and the cookie is used for comparison and verification. This user can change the effect and verification code length according to their preferences (:

Effect

CodeAs follows:
Checkcode. aspx

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 a random Generator
Random random = new random ();

// Clear the background color of the image
G. Clear (color. White );

// Draw 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
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 of the image
G. 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 his verification code is saved to the session. 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;

/** //


// The website verification code generation function of validateimagehandler
///
public class validateimagehandler: ihttphandler, irequiressessionstate
{< br> int intlength = 5; // length
string stridentify = "identify"; // key value stored in random strings, stored in session
Public validateimagehandler ()
{< BR >}

/** //


// generate the verification image core code
///
///
Public void processrequest (httpcontext HC)
{< br> // 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 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 ";
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 lines
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 (); // The session is saved first to verify that it is consistent with the user input.
HC. response. End ();

}

/** // <Summary>
/// Indicates whether such instances can be shared by multiple requests (reuse can improve performance)
/// </Summary>
Public bool isreusable
{
Get
{
Return true;
}
}

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.