Home Backend Development C#.Net Tutorial asp.net generates verification code (Pure Chinese)

asp.net generates verification code (Pure Chinese)

Jan 13, 2017 pm 02:34 PM
Verification code

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: 在此处添加构造函数逻辑 
// 
} 
private static object[] CreateString() 
{ 
//定义一个数组存储汉字编码的组成元素 
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(); //定义一个随机数对象 
object[] bytes = new object[4]; 
for (int i = 0; i < 4; i++) 
{ 
//获取区位码第一位 
int ran1 = ran.Next(11, 14); 
string str1 = str[ran1].Trim(); 
//获取区位码第二位并防止数据重复 
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(); 
//获取区位码第三位 
ran = new Random(ran2 * unchecked((int)DateTime.Now.Ticks) + i); 
int ran3 = ran.Next(10, 16); 
string str3 = str[ran3].Trim(); 
//获取区位码第四位 
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(); 
//定义字节变量存储产生的随机汉字区位码 
byte byte1 = Convert.ToByte(str1 + str2, 16); 
byte byte2 = Convert.ToByte(str3 + str4, 16); 
byte[] stradd = new byte[] { byte1, byte2 }; 
//将产生的汉字字节放入数组 
bytes.SetValue(stradd, i); 
} 
return bytes; 
} 
private static string GetString() 
{ 
Encoding gb = Encoding.GetEncoding("gb2312"); 
object[] bytes = CreateString(); 
//根据汉字字节解码出中文汉字 
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); //创建画布 
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); 
} 
} 
}
Copy after login

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(); //调用方法生成四位汉字验证码 
} 
}
Copy after login

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+&#39;?&#39;" src="ChineseCheckCode.aspx" 
style="width: 75px; height: 24px" align="left" /> 
<asp:ImageButton ID="imgBtnLogin" runat="server" ImageUrl="~/Images/Login.GIF" 
OnClick="imgBtnLogin_Click" />
Copy after login

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(&#39;验证码输入错误,请重新输入!&#39;);Location=&#39;ChineseCodeValidator.aspx&#39;</script>"); 
return; 
} 
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? What should I do if Google Chrome does not display the verification code image? Chrome browser does not display the verification code? Mar 13, 2024 pm 08:55 PM

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

PHP image processing case: How to implement the verification code function of images PHP image processing case: How to implement the verification code function of images Aug 17, 2023 pm 12:09 PM

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.

Can virtual numbers receive verification codes? Can virtual numbers receive verification codes? Jan 02, 2024 am 10:22 AM

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.

Why can't I receive the verification code on my phone? Why can't I receive the verification code on my phone? Aug 17, 2023 pm 02:49 PM

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.

Verification codes can't stop robots! Google AI can accurately identify blurry text, while GPT-4 pretends to be blind and asks for help Verification codes can't stop robots! Google AI can accurately identify blurry text, while GPT-4 pretends to be blind and asks for help Apr 12, 2023 am 09:46 AM

“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 Development Guide: Implementing Verification Code Login PHP Development Guide: Implementing Verification Code Login Jul 01, 2023 am 09:27 AM

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 create a verification code image using PHP? How to create a verification code image using PHP? Sep 13, 2023 am 11:40 AM

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.

Use OCR technology to automatically identify various verification codes, and the tool has been open source Use OCR technology to automatically identify various verification codes, and the tool has been open source May 25, 2023 am 10:07 AM

Today I am sharing with you an OCR application - ddddocr automatically recognizes verification codes. The first four d's are the first pinyin of "daidai younger brother". [/Laughter]. Project address: https://github.com/sml2h3/ddddocr. When using it, use the pip command to install it directly, just pipinstalldddddocr. The core technology of OCR includes two aspects. One is the target detection model to detect the text in the picture, and the other is the text recognition model to convert the text in the picture into text. The first type of verification codes is the simplest. They do not have complex background images, so the target detection model can be omitted and the images can be directly sent to the text recognition model. The identification code is as follows: impor

See all articles