asp.net Generate verification code (pure Chinese) _ Practical Tips

Source: Internet
Author: User
Tags httpcontext
Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
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.Text; Adding references
Using System.Drawing; Adding references
<summary>
Summary description of Checkcode_ch
</summary>
public class Checkcode_ch
{
Public Checkcode_ch ()
{
//
TODO: Add constructor logic here
//
}
private static object[] createstring ()
{
Defines the constituent elements of an array storage encoding
string[] str = new STRING[16] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};
Random ran = new Random (); Define a random Number object
object[] bytes = new Object[4];
for (int i = 0; i < 4; i++)
{
Get the first place in the location code
int ran1 = ran. Next (11, 14);
String str1 = Str[ran1]. Trim ();
Get the second digit of the location code and prevent data duplication
ran = new Random (Ran1 * Unchecked ((int) DateTime.Now.Ticks) + i);
int ran2;
if (Ran1 = 13)
{
Ran2 = ran. Next (0, 7);
}
Else
{
Ran2 = ran. Next (0, 16);
}
String str2 = Str[ran2]. Trim ();
Get Location Code third place
ran = new Random (ran2 * Unchecked ((int) DateTime.Now.Ticks) + i);
int ran3 = ran. Next (10, 16);
String str3 = Str[ran3]. Trim ();
Get Location Code fourth place
ran = new Random (RAN3 * Unchecked ((int) DateTime.Now.Ticks) + i);
int ran4;
if (ran3 = 10)
{
Ran4 = ran. Next (1, 16);
}
else if (ran3 = 15)
{
Ran4 = ran. Next (0, 15);
}
Else
{
Ran4 = ran. Next (0, 16);
}
String STR4 = Str[ran4]. Trim ();
Define the random Chinese character location code generated by byte variable storage
byte byte1 = convert.tobyte (str1 + str2, 16);
byte Byte2 = convert.tobyte (Str3 + STR4, 16);
byte[] Stradd = new byte[] {byte1, byte2};
Put the resulting character byte into an array
bytes. SetValue (Stradd, i);
}
return bytes;
}
private static string GetString ()
{
Encoding GB = encoding.getencoding ("gb2312");
object[] bytes = createstring ();
Decoding Chinese characters according to the byte of Chinese characters
String str1 = GB. GetString ((byte[]) Convert.changetype (Bytes[0], typeof (byte[)));
String str2 = GB. GetString ((byte[]) Convert.changetype (Bytes[1], typeof (byte[)));
String str3 = GB. GetString ((byte[]) Convert.changetype (bytes[2], typeof (byte[)));
String STR4 = GB. GetString ((byte[]) Convert.changetype (Bytes[3], typeof (byte[)));
String str = str1 + str2 + str3 + str4;
HTTPCONTEXT.CURRENT.RESPONSE.COOKIES.ADD (New HttpCookie ("Checkcode", str));
return str;
}
public static void Graphicsimage ()
{
System.Drawing.Bitmap image = new System.Drawing.Bitmap ((int) math.ceiling (GetString (). Length * 22.5)), 22);
Graphics g = graphics.fromimage (image); Create Canvas
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 < 1; 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 ("Couriew new", 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 (GetString (), 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 ();
Image. Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF);
HttpContext.Current.Response.ClearContent ();
HttpContext.Current.Response.ContentType = "Image/gif";
HttpContext.Current.Response.BinaryWrite (Ms. ToArray ());
}
catch (Exception ms)
{
HttpContext.Current.Response.Write (Ms. message);
}
}
}

The second step is to establish a page reference class library chinesecheckcode.aspx the foreground without writing code, background reference class library.
Copy Code code as follows:

Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
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;
public partial class UserValidator_ChineseCheckCode:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Checkcode_ch.graphicsimage (); Invoke method to generate four-bit Chinese character verification code
}
}

Third Step Reference Verification code page
Copy Code code as follows:

<asp:textbox id= "Validator" runat= "Server" width= "150px" ></asp:TextBox>
Style= "WIDTH:75PX; Height:24px "align=" left "/>
<asp:imagebutton id= "Imgbtnlogin" runat= "Server" imageurl= "~/images/login.gif"
onclick= "Imgbtnlogin_click"/>

Background judgment
Copy Code code as follows:

protected void Imgbtnlogin_click (object sender, ImageClickEventArgs e)
{
HttpCookie cookie = request.cookies["Checkcode"];
if (cookie). Value = = this. Validator.Text.Trim ())
{
。。。
}
Else
{
Response.Write (' <script>alert (' Authentication code input error, please re-enter! '); location= ' chinesecodevalidator.aspx ' </script> ');
Return
}
}

The above verification code generates four digits, please make appropriate changes according to the situation.
Now we have summed up the verification code technology for generating pure digit, digital alphabet mixture and pure Chinese character. I hope to help you.
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.