Asp.net| Verification Code
The first step is to generate the verification code
Add a validatecode.aspx page
The code in ValidateCode.aspx.cs 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.Imaging;
public partial class ValidateCode:System.Web.UI.Page
{//This page will be used to generate the CAPTCHA picture
protected void Page_Load (object sender, EventArgs e)
{//Calling function to generate a picture of a captcha
This. Createcheckcodeimage (Generatecheckcode ());
}
private String Generatecheckcode ()
{//Generate five-bit random string
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));
session["Checkcode"] = checkcode;//for client parity code comparisons
return checkcode;
}
private void Createcheckcodeimage (string checkcode)
{///generate a picture display for the verification code
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 picture
for (int i = 0; i < 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 < i++)
{
int x = random. Next (image. Width);
int y = random. Next (image. Height);
Image. SetPixel (x, Y, Color.FromArgb) (random. Next ()));
}
//Picture border lines
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.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}
Second Step Test verification code
Establish test.aspx
Form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:button id= "Button1" runat= "Server" text= "button"/><br
<asp:image id= "Image1" runat= "Server" imageurl= "~/validatecode.aspx"/>//the picture will be used to display the verification code
</div>
</form>
Test.aspx.cs Code
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
if (this. TextBox1.Text.ToString (). Trim () = = session["Checkcode"]. ToString ()) {Response.Write ("<script lauguage= ' JavaScript ' >alert (' verify success ');</script>");}
}