Asp.net generate Verification Code (only numbers)

Source: Internet
Author: User

CheckCode. cs Copy codeThe Code is as follows: using System;
Using System. Data;
Using System. Configuration;
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;
/// <Summary>
/// Summary of CheckCode
/// </Summary>
Public class CheckCode
{
Public CheckCode ()
{
//
// TODO: add the constructor logic here
//
}
Public static void DrawImage ()
{
CheckCode img = new CheckCode ();
HttpContext. Current. Session ["CheckCode"] = img. RndNum (4 );
Img. checkCodes (HttpContext. Current. Session ["CheckCode"]. ToString ());
}
/// <Summary>
/// Generate verification Image
/// </Summary>
/// <Param name = "checkCode"> Verification character </param>
Private void checkCodes (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>
Private string 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;
}
}

Create a page checkCode. aspx that references the class. The front end does not need to write anything. The back end just needs to reference the DrawImage () method of the class we created.Copy codeThe 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;
Public partial class checkCode: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
CheckCode. DrawImage ();
}
}

Next we can reference the checkCode. aspx page on the page where the verification code is required.
Front-endCopy codeThe Code is as follows: <asp: TextBox ID = "Validator" runat = "server" Width = "150px"> </asp: TextBox>
Style = "width: 73px; height: 22px" align = "left"/>
<Asp: ImageButton ID = "imgBtnLogin" runat = "server" ImageUrl = "~ /Images/Login. GIF"
OnClick = "imgBtnLogin_Click"/>

Background judgmentCopy codeThe Code is as follows: protected void imgBtnLogin_Click (object sender, ImageClickEventArgs e)
{
If (this. Validator. Text = Session ["CheckCode"]. ToString ())
{
//....
}
Else
{
Response. Write ("<script> alert ('verification code entered incorrectly. Please enter it again! '); Location = 'mumbervalidate. aspx' </script> ");
Return;
}
}

Modify the code as needed.

Related Article

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.