Effect:
Default. aspx
Copy codeThe Code is 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 codeThe Code is 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>
/// Generate a random 4-digit number
/// </Summary>
/// <Returns> returns the generated random number </returns>
Public string GetVali ()
{
String strsvali = ";
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 passed the logon verification! ') </Script> ");
}
Else
{
ClientScript. RegisterStartupScript (this. GetType (), "ss", "<script> alert ('the verification code you entered is incorrect! ') </Script> ");
}
}
}
GetValid. aspx
(You can directly assign this page as the source to ImageUrl)
The foreground is blank. The background code is as follows:
Copy codeThe Code is 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 (); // generates four random strings
CreateImage (validateNum); // map the generated random string to an image
Session ["ValidNums"] = validateNum; // Save the verification code
}
}
Public static string GetValids ()
{
// Obtain the GB2312 encoding page (table)
Encoding gb = Encoding. GetEncoding ("gb2312 ");
// Call the function to generate four random Chinese character codes
Object [] bytes = CreateRegionCode (4 );
// Decodes Chinese Characters Based on the byte array encoded by Chinese Characters
String s = String. Empty;
Foreach (object byt in bytes)
{
String str1 = gb. GetString (byte []) Convert. ChangeType (byt, typeof (byte []);
S = s + str1;
}
// Output Console
Return s;
}
Public static object [] CreateRegionCode (int strlength)
{
// Define a string array to store the components of Chinese character 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 ();
// Define an object array
Object [] bytes = new object [strlength];
/* Generate a hexadecimal byte array containing two elements at a time in each loop, and put it into the bject array.
Each Chinese Character consists of four location codes.
The first element of the byte array is the 1st bits and 2nd bits.
The second element of the byte array is the 3rd bits and 4th bits.
*/
For (int I = 0; I <strlength; I ++)
{
// Code 1st bits
Int r1 = rnd. Next (11, 14 );
String str_r1 = rBase [r1]. Trim ();
// Code 2nd bits
Rnd = new Random (r1 * unchecked (int) DateTime. Now. Ticks) + I );
// Replace the seed of the random number generator to avoid repeated values
Int r2;
If (r1 = 13)
{
R2 = rnd. Next (0, 8 );
}
Else
{
R2 = rnd. Next (0, 16 );
}
String str_r2 = rBase [r2]. Trim ();
// Code 3rd bits
Rnd = new Random (r2 * unchecked (int) DateTime. Now. Ticks) + I );
Int r3 = rnd. Next (10, 16 );
String str_r3 = rBase [r3]. Trim ();
// Code 4th bits
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 ();
// Define the random Chinese character location code generated by storing two byte Variables
Byte byte1 = Convert. ToByte (str_r1 + str_r2, 16 );
Byte byte2 = Convert. ToByte (str_r3 + str_r4, 16 );
// Store two byte variables in the byte array
Byte [] str_r = new byte [] {byte1, byte2 };
// Put the byte array of the generated Chinese character into the object Array
Bytes. SetValue (str_r, I );
}
Return bytes;
}
// Generate an image
Private void CreateImage (string validateNum)
{
If (validateNum = null | validateNum. Trim () = String. Empty)
Return;
// Generate a Bitmap image
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 ();
// Clear the background color of the image
G. Clear (Color. White );
// Draw the background noise line of the image
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. 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 );
// Foreground noise of the image
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 image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
// Save the image to the 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 directly assign this page as the source to ImageUrl)
The foreground is blank. The background code is as follows:
Copy codeThe Code is 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); // generates four random strings
CreateImage (validateNum); // map the generated random string to an image
Session ["ValidNums"] = validateNum; // Save the verification code
}
}
// Generate a random string
Private string CreateRandomNum (int NumCount)
{
String allChar = ", 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 it into an array
String randomNum = "";
Int temp =-1; // record the value of the last random number. Try to avoid 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 an image
Private void CreateImage (string validateNum)
{
If (validateNum = null | validateNum. Trim () = String. Empty)
Return;
// Generate a Bitmap image
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 ();
// Clear the background color of the image
G. Clear (Color. White );
// Draw the background noise line of the image
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. Coral), 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 (validateNum, font, brush, 2, 2 );
// Foreground noise of the image
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 image
G. DrawRectangle (new Pen (Color. Silver), 0, 0, image. Width-1, image. Height-1 );
System. IO. MemoryStream MS = new System. IO. MemoryStream ();
// Save the image to the 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 are all the codes for the three verification codes.
In addition, you can also use the ashx request verification code. In this example, only the Session value is used for processing.