Home>

Many websites now have registration and login pages,In order to better satisfy the user experience and website security,Many websites use dynamic generated graphic codes or additional codes for verification.The method of generating verification codes is organized as follows.

The implementation of verification technology is to generate a random number on the server side.And save it in memory,Send to the browser,And submit it to the user as an image.During the project,When completing a verification code for user registration and login using a script,I found various ways to generate verification codes,Here are several different ways to generate verification codes:

1.Draw a pure digital website verification code

This example implements digital verification code technology.That is, a 4-digit number is randomly generated as a verification code.Digital verification code technology can be used in the development and drawing of member login verification modules.

■ Design process

Write a method to generate a digital verification code in a new form checkcode.aspx:

private string rndnum ()
 {
 int number;
 char code;
 string checkcode=string.empty;
 system.random random=new random ();
 for (int i=0;i<4;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 ("yzmcode", 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 picture
 g.clear (color.white);
 //Draw the background noise line of the picture
 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", 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);
 //Draw the foreground noise point of the picture
 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 the border line of the picture
 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 ();
 }
 }

Then call createcheckcodeimage () in page_load:

protected void page_load (object sender, eventargs e)
 {
 this.createcheckcodeimage (rndnum ());
}
Then we can use simple controls in the window we log in:
<asp:textbox runat="server"></asp:textbox>
 <asp:imagebutton runat="server" imageurl="~/checkcode.aspx" width="50" height="30" /<asp:label
 runat="server" text="Can't see clearly?Click on the picture to change it to" height="16px"
 font-size="small" forecolor="red"</asp:label>
 <br />
 <asp:button runat="server" onclick="button1_click" text="button" />
Determine the correctness of the verification code input in button1_click:
protected void button1_click (object sender, eventargs e)
{
 if (string.compare (request.cookies ["yzmcode"]. value, textbox1.text, true)!=0)
 {
 response.write ("<script>alert (" Verification code error! ")</script>");
 }
 else
 response.write ("<script>alert (" Verification code is correct! ")</script>");
}

2. Website verification codes that draw numbers and letters

Very similar to pure numbers,The specific difference is in the method of randomly generating strings

private string generatecheckcode ()
 {
 int number;
 char code;
 string checkcode=string.empty;
 random random=new random ();
 for (int i=0;i<4;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;
 }

After the string is generated,The next step is to draw the string into an image and display it.code show as below:

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 picture
 g.clear (color.white);
 //Draw the background noise line of the picture
 for (int i=0;i<2;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.black), x1, y1, x2, y2);
 }
 font font=new system.drawing.font ("arial", 12, (system.drawing.fontstyle.bold));
 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);
 //Draw the foreground noise point of the picture
 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 the border line of the picture
 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 ();
 }
 }

Check whether the verification code is entered correctly in the login window:

protected void button1_click (object sender, eventargs e)
 {
 httpcookie cookie=request.cookies ["checkcode"];
 if (cookie.value == this.textbox1.text.trim ())
 {
 response.write ("<script>alert (" Verification code is correct! ")</script>");
 }
 else
 {
 response.write ("<script>alert (" Verification code error! ")</script>");
 }
 }

Then a website verification code about the combination of numbers and letters is generated.

3. Four verification codes

Other verification codes are similar to numbers and letters.I will not write anymore,The specific difference is in the method of randomly generating four arithmetic expressions.as follows:

private string generatecheckcode () { random rd=new random (); int results=0; int num1=rd.next (10); int num2=rd.next (10); string expressions=""; int f=(rd.next (4) + 1); switch (f) { case 1: results=num1 + num2; expressions=num1 + "+" + num2; break; case 2: results=num1-num2; expressions=num1 + "-" + num2; break; case 3: results=num1 * num2; expressions=num1 + "*" + num2; break; case 4: if (num2>0) { results=convert.toint16 (num1/num2); expressions=num1 + "/" + num2; } else { results=num1; expressions=num1 + "/1"; } break; } session ["code"]=results.tostring (); return expressions;}

c
  • Previous winform simple cache class instance
  • Next MYSQL create function error solution