The effect is as shown in the figure:
Default.aspx
Copy Code code as follows:
<table>
<tr>
<TD class= "Style1" >
(Verification Code test) </td>
<td>
<asp:label
Id= "Label1" runat= "Server" ></asp:Label>
<asp:image id= "Image1" runat= "Server" height= "22px" imageurl= "~/validnums.aspx" width= "58px"/>
<asp:image id= "Image2" runat= "Server" height= "22px" imageurl= "~/getvalid.aspx" width= "58px"/></td>
</tr> <tr>
<TD class= "Style1" >
</td>
<td>
<asp:button id= "Button1" runat= "server" text= "login" onclick= "Btnok_click"/>
<asp:button id= "Button2" runat= "Server" text= "Cancel"/>
</td>
</tr>
</table>
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
String getnums = Getvali ();
Label1.Text = Getnums;
}
}
<summary>
Randomly generated 4-digit number
</summary>
<returns> returns the generated random number </returns>
public string Getvali ()
{
String Strsvali = "0,1,2,3,4,5,6,7,8,9";
string[] Valiarray = Strsvali. Split (', ');
String returnnum = "";
int nums =-1;
Random Vrand = new Random ();
for (int n = 1; n < 5; n++)
{
if (nums!=-1)
{
Vrand = new Random (n * nums * Unchecked ((int) DateTime.Now.Ticks));
}
int t = Vrand. Next (10);
Nums = t;
Returnnum + = valiarray[t];
}
session["Valid"] = Returnnum;
return returnnum;
}
protected void Btnok_click (object sender, EventArgs e)
{
if (session["Valid"]. ToString () = = TextBox3.Text)
{
Clientscript.registerstartupscript (this. GetType (), "SS", "<script>alert" (' You have successfully authenticated through login! ') </script> ");
}
Else
{
Clientscript.registerstartupscript (this. GetType (), "SS", "<script>alert" (' you entered a CAPTCHA error! ') </script> ");
}
}
}
getvalid.aspx
(You can assign the page as a source directly to ImageUrl)
The foreground is empty and the background code is as follows:
Copy Code code as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Text;
Using System.Drawing;
public partial class GetValid:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
String validatenum = Getvalids (); Genetic 4-bit random string
CreateImage (Validatenum); Draw a generated random string into a picture
session["validnums"] = Validatenum; Save Verification Code
}
}
public static string Getvalids ()
{
Get GB2312 Encoding page (table)
Encoding GB = encoding.getencoding ("gb2312");
Call function generates 4 random Chinese encoding
object[] bytes = Createregioncode (4);
Decoding Chinese characters according to the encoding byte array
string s = String.Empty;
foreach (Object byt in bytes)
{
String str1 = GB. GetString ((byte[]) Convert.changetype (Byt, typeof (byte[)));
s = s + str1;
}
Console for output
return s;
}
public static object[] Createregioncode (int strlength)
{
Defines a string array that stores the constituent elements of the encoding
string[] Rbase = new String[16] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};
Random rnd = new Random ();
Defines an object array to
object[] bytes = new Object[strlength];
/* Each loop produces a two-element hexadecimal byte array at a time and puts it into the bject array
Each Chinese character is composed of four location codes.
Location Code 1th and location code 2nd bit as the first element of a byte array
Location Code 3rd and Location Code 4th digits as the second element of a byte array
*/
for (int i = 0; i < strlength; i++)
{
Location Code 1th Place
int r1 = rnd. Next (11, 14);
String str_r1 = Rbase[r1]. Trim ();
Location Code 2nd place
Rnd = new Random (R1 * Unchecked ((int) DateTime.Now.Ticks) + i);
Replace the seed of the random number generator to avoid duplicate values
int R2;
if (r1 = 13)
{
r2 = rnd. Next (0, 8);
}
Else
{
r2 = rnd. Next (0, 16);
}
String str_r2 = Rbase[r2]. Trim ();
Location Code 3rd place
Rnd = new Random (R2 * Unchecked ((int) DateTime.Now.Ticks) + i);
int r3 = rnd. Next (10, 16);
String str_r3 = Rbase[r3]. Trim ();
Location Code 4th place
Rnd = new Random (R3 * Unchecked ((int) DateTime.Now.Ticks) + i);
int R4;
if (R3 = 10)
{
R4 = rnd. Next (1, 16);
}
else if (r3 = 15)
{
R4 = rnd. Next (0, 15);
}
Else
{
R4 = rnd. Next (0, 16);
}
String str_r4 = Rbase[r4]. Trim ();
Defines a random Chinese character location code generated by two byte variable storage
byte byte1 = convert.tobyte (str_r1 + str_r2, 16);
byte Byte2 = convert.tobyte (Str_r3 + STR_R4, 16);
Storing two byte variables in a byte array
byte[] Str_r = new byte[] {byte1, byte2};
Puts a byte array of the resulting Chinese character into an object array
bytes. SetValue (Str_r, i);
}
return bytes;
}
Generate pictures
private void CreateImage (string validatenum)
{
if (Validatenum = null | | Validatenum.trim () = String.Empty)
Return
Generating bitmap images
System.Drawing.Bitmap image = New System.Drawing.Bitmap (validatenum.length * 12 + 10, 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 < 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.coral), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font ("Arial", 8);
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 (validatenum, 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 ()));
}
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 ();
To save an image to a specified stream
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}
validnums.aspx
(You can assign the page as a source directly to ImageUrl)
The foreground is empty and the background code is as follows:
Copy Code code as follows:
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Drawing;
public partial class ValidNums:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
String validatenum = Createrandomnum (4); Genetic 4-bit random string
CreateImage (Validatenum); Draw a generated random string into a picture
session["validnums"] = Validatenum; Save Verification Code
}
}
Generate a random string
private string createrandomnum (int numcount)
{
String Allchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,w,x,y,z";
string[] Allchararray = Allchar.split (', ');//split into an array
String randomnum = "";
int temp = -1;//records the last random number and tries to avoid generating several identical random numbers
Random rand = new Random ();
for (int i = 0; i < Numcount; i++)
{
if (temp!=-1)
{
Rand = new Random (i * temp * ((int) DateTime.Now.Ticks));
}
int t = rand. Next (35);
if (temp = = t)
{
Return Createrandomnum (Numcount);
}
temp = t;
Randomnum + = allchararray[t];
}
return randomnum;
}
Generate pictures
private void CreateImage (string validatenum)
{
if (Validatenum = null | | Validatenum.trim () = String.Empty)
Return
Generating bitmap images
System.Drawing.Bitmap image = New System.Drawing.Bitmap (validatenum.length * 12 + 10, 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 < 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.coral), 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 (validatenum, 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 ()));
}
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 ();
To save an image to a specified stream
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
Response.clearcontent ();
Response.ContentType = "Image/gif";
Response.BinaryWrite (Ms. ToArray ());
}
Finally
{
G.dispose ();
Image. Dispose ();
}
}
}
The above is the entire code of 3 kinds of authentication codes.
In addition, you can use the ASHX request verification code, this example uses only the session stored value processing.