Asp. Net Dynamic generation of Verification Code

Source: Internet
Author: User

We often use verification codes when Designing User Login modules to effectively prevent malicious cracking of hacker software. The source code of common verification codes is published to generate results.


Usage:
1. Add a class in a Web project, such as "CreateImage. cs", and Copy the published source code;
2. Create a new Web form, such as "Image. aspx". Add the code "CreateImage. DrawImage ();" to Page_Load. Do not forget to add references to the class !!
3. Add the following javascript code to the appropriate location on the page (where you want to place the verification code,
<Script language = "JavaScript">
<! --
Var numkey = Math. random ();
Numkey = Math. round (numkey * 10000 );
Document. write ("// -->
</SCRIPT>

The source code is as follows:
/// <Summary>
/// Verification Code Module
/// </Summary>
Publicclass CreateImage
{
Publicstaticvoid DrawImage ()
{
CreateImage img = new CreateImage ();
HttpContext. Current. Session ["CheckCode"] = img. RndNum (4 );
Img. CreateImages (HttpContext. Current. Session ["CheckCode"]. ToString ());
}

/// <Summary>
/// Generate verification Image
/// </Summary>
/// <Param name = "checkCode"> Verification character </param>
Privatevoid CreateImages (string checkCode)
{
Int iwidth = (int) (checkCode. Length * 13 );
System. Drawing. Bitmap image = new System. Drawing. Bitmap (iwidth, 23 );
Graphics g = Graphics. FromImage (image );
G. Clear (Color. White );
// Define the color
Color [] c = {Color. Black, Color. Red, Color. DarkBlue, Color. Green, Color. Orange, Color. Brown, Color. DarkCyan, Color. Purple };
// Define the font
String [] font = {"Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", ""};
Random rand = new Random ();
// Random output noise
For (int I = 0; I <50; I ++)
{
Int x = rand. Next (image. Width );
Int y = rand. Next (image. Height );
G. DrawRectangle (new Pen (Color. LightGray, 0), x, y, 1, 1 );
}

// Output verification code characters of different fonts and colors
For (int I = 0; I <checkCode. Length; I ++)
{
Int cindex = rand. Next (7 );
Int findex = rand. Next (5 );

Font f = new System. Drawing. Font (font [findex], 10, System. Drawing. FontStyle. Bold );
Brush B = new System. Drawing. SolidBrush (c [cindex]);
Int ii = 4;
If (I + 1) % 2 = 0)
{
Ii = 2;
}
G. DrawString (checkCode. Substring (I, 1), f, B, 3 + (I * 12), ii );
}
// Draw a border
G. DrawRectangle (new Pen (Color. Black, 0), 0, 0, image. Width-1, image. Height-1 );

// Output to the browser
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
Image. Save (MS, System. Drawing. Imaging. ImageFormat. Jpeg );
HttpContext. Current. Response. ClearContent ();
// Response. ClearContent ();
HttpContext. Current. Response. ContentType = "image/Jpeg ";
HttpContext. Current. Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Image. Dispose ();
}

/// <Summary>
/// Generate random letters
/// </Summary>
/// <Param name = "VcodeNum"> Number of Generated letters </param>
/// <Returns> string </returns>
Privatestring RndNum (int VcodeNum)
{
String Vchar = ";
String [] VcArray = Vchar. Split (',');
String VNum = ""; // StringBuilder is not required because the string is short.
Int temp =-1; // record the previous random value. Avoid producing several identical random numbers.

// Use a simple algorithm to ensure different random numbers are generated
Random rand = new Random ();
For (int I = 1; I <VcodeNum + 1; I ++)
{
If (temp! =-1)
{
Rand = new Random (I * temp * unchecked (int) DateTime. Now. Ticks ));
}
Int t = rand. Next (VcArray. Length );
If (temp! =-1 & temp = t)
{
Return RndNum (VcodeNum );
}
Temp = t;
VNum + = VcArray [t];
}
Return VNum;
}
}

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/21aspnet/archive/2007/03/19/1534319.aspx

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.