asp.net Verification Code Implementation code (C #) _ Practical Tips

Source: Internet
Author: User
Tags generator
public class ValidateCode:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
This. Createcheckcodeimage (Generatecheckcode ());
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
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 ();
Empty picture background color
G.clear (Color.White);
Background noise line for painting pictures
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.silver), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font (Arial), (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);
Picture of the foreground noise point
for (int i=0; i<100; i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}
Draw a picture's border line
G.drawrectangle (New Pen (Color.silver), 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.c;
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}


If the above Authentication code generator page name is: checkcode.aspx, then uses "
Use the following code to determine the authentication code in the handling event of the login button on the login page:

private void Btnlogin_click (object sender, System.Web.UI.ImageClickEventArgs e)
{
if (request.cookies["checkcode"] = = null)
{
Lblmessage.text = "Your browser settings have been disabled cookies, you must set the browser to allow the use of cookies to use the system." ";
Lblmessage.visible = true;
Return
}
if (String.Compare (request.cookies["Checkcode"). Value, Txtcheckcode.text, True)!= 0)
{
Lblmessage.text = "Wrong authentication code, please enter the correct authentication code." ";
Lblmessage.visible = true;
Return
}

}
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.