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; //添加引用 using System.Drawing; //添加引用 /// <summary> /// CheckCode_Ch 的摘要說明 /// </summary> public class CheckCode_Ch { public CheckCode_Ch() { // // TODO: 在此處添加構造函數(shù)邏輯 // } private static object[] CreateString() { //定義一個數(shù)組存儲漢字編碼的組成元素 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(); //定義一個隨機數(shù)對象 object[] bytes = new object[4]; for (int i = 0; i < 4; i++) { //獲取區(qū)位碼第一位 int ran1 = ran.Next(11, 14); string str1 = str[ran1].Trim(); //獲取區(qū)位碼第二位并防止數(shù)據重復 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(); //獲取區(qū)位碼第三位 ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i); int ran3 = ran.Next(10, 16); string str3 = str[ran3].Trim(); //獲取區(qū)位碼第四位 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(); //定義字節(jié)變量存儲產生的隨機漢字區(qū)位碼 byte byte1 = Convert.ToByte(str1 + str2, 16); byte byte2 = Convert.ToByte(str3 + str4, 16); byte[] stradd = new byte[] { byte1, byte2 }; //將產生的漢字字節(jié)放入數(shù)組 bytes.SetValue(stradd, i); } return bytes; } private static string GetString() { Encoding gb = Encoding.GetEncoding("gb2312"); object[] bytes = CreateString(); //根據漢字字節(jié)解碼出中文漢字 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); //創(chuàng)建畫布 try { //生成隨機生成器 Random random = new Random(); //清空圖片背景色 g.Clear(Color.White); //畫圖片的背景噪音線 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", 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(GetString(), font, brush, 2, 2); //畫圖片的前景噪音點 for (int i = 0; i < 50; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //畫圖片的邊框線 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 create a page that references the class library ChineseCheckCode.aspx. There is no need to write code in the front end, and the class library is referenced in the background. .
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(); //調用方法生成四位漢字驗證碼 } }
The third step refers to the verification code page
<asp:TextBox ID="Validator" runat="server" Width="150px" ></asp:TextBox> <img id="Img1" alt="看不清,請點擊我!" onclick="this.src=this.src+'?'" src="ChineseCheckCode.aspx" style="width: 75px; height: 24px" align="left" /> <asp:ImageButton ID="imgBtnLogin" runat="server" ImageUrl="~/Images/Login.GIF" OnClick="imgBtnLogin_Click" />
Backend judgment
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('驗證碼輸入錯誤,請重新輸入!');Location='ChineseCodeValidator.aspx'</script>"); return; } }
The above verification code is generated with four digits. Please make appropriate modifications according to the situation.
Now we summarize the verification code technology for generating pure numbers, mixed numbers and letters, and pure Chinese characters. Hope it helps you. .
For more asp.net generated verification code code (pure Chinese) related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What should I do if Google Chrome does not display the verification code image? Sometimes you need a verification code to log in to a web page using Google Chrome. Some users find that Google Chrome cannot display the content of the image properly when using image verification codes. What should be done? The editor below will introduce how to deal with the Google Chrome verification code not being displayed. I hope it will be helpful to everyone! Method introduction: 1. Enter the software, click the "More" button in the upper right corner, and select "Settings" in the option list below to enter. 2. After entering the new interface, click the "Privacy Settings and Security" option on the left. 3. Then click "Website Settings" on the right

The virtual number can receive the verification code. As long as the mobile phone number filled in during registration complies with the regulations and the mobile phone number can be connected normally, you can receive the SMS verification code. However, you need to be careful when using virtual mobile phone numbers. Some websites do not support virtual mobile phone number registration, so you need to choose a regular virtual mobile phone number service provider.

“The most annoying thing is all kinds of weird (or even perverted) verification codes when you log into a website.” Now, there is good news and bad news. The good news is: AI can do this for you. If you don’t believe me, here are three real cases of increasing recognition difficulty: And these are the answers given by a model called “Pix2Struct”: Are they all accurate and word for word? Some netizens lamented: Sure, the accuracy is better than mine. So can it be made into a browser plug-in? ? Yes, some people said: Even though these cases are relatively simple, if you just fine-tune it, I can't imagine how powerful the effect will be. So, the bad news is - the verification code will soon be unable to stop the robots! (Danger danger danger...) How to do it? Pix2St

PHP image processing case: How to implement the verification code function of images. With the rapid development of the Internet, verification codes have become one of the important means to protect website security. Verification code is a verification method that uses image recognition technology to determine whether the user is a real user. This article will introduce how to use PHP to implement the verification code function of images, and come with code examples. Introduction A verification code is a picture containing random characters. The user needs to enter the characters in the picture to pass the verification. The main process of implementing verification code includes generating random characters and drawing characters into pictures.

How to create a verification code image using PHP? CAPTCHA is a commonly used method to verify whether the user is a human and not a machine. On websites, we often see verification code images, which require users to enter random characters or numbers displayed on the image to complete operations such as login, registration, and commenting. This article will introduce how to use PHP to create a verification code image and provide specific code examples. 1. PHPGD library To create a verification code image, we need to use PHP's GD library. The GD library is an extension for processing images.

Failure to receive the verification code on your mobile phone is caused by network problems, mobile phone settings problems, mobile phone operator problems and personal settings problems. Detailed introduction: 1. Network problems. The network environment where the mobile phone is located is unstable or the signal is weak, which may cause the verification code to be unable to be delivered in time; 2. Mobile phone setting problems. The text message or voice function of the mobile phone is accidentally turned off, or the The verification code sending number is added to the blacklist, resulting in the verification code not being received normally; 3. Mobile phone operator issues, the mobile phone operator may have malfunctions or maintenance, resulting in the verification code not being delivered in time, etc.

With the development of the Internet and the popularity of smartphones, the verification code login function is adopted by more and more websites and applications. Verification code login is a login method that verifies the user's identity by entering the correct verification code to improve security and prevent malicious attacks. In PHP development, implementing a simple verification code login function is not complicated and can be completed through the following steps. Create a database table First, we need to create a table in the database to store verification code information. The table structure can contain the following fields: id: auto-incrementing primary key phon

How to use JavaScript to implement the verification code function? With the development of the Internet, verification codes have become one of the indispensable security mechanisms in websites and applications. Verification code (VerificationCode) is a technology used to determine whether the user is a human rather than a machine. With CAPTCHAs, websites and applications can prevent spam submissions, malicious attacks, bot crawlers, and more. This article will introduce how to use JavaScript to implement the verification code function and provide specific code
