search
HomeBackend DevelopmentC#.Net Tutorialasp.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); 
} 
} 
}

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 src="/static/imghwm/default1.png"  data-src="ChineseCheckCode.aspx"  class="lazy"  id="Img1" alt="看不清,请点击我!" onclick="this.src=this.src+&#39;?&#39;"  
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(&#39;验证码输入错误,请重新输入!&#39;);Location=&#39;ChineseCodeValidator.aspx&#39;</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!

Statement
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
C# .NET Development Today: Trends and Best PracticesC# .NET Development Today: Trends and Best PracticesApr 28, 2025 am 12:25 AM

The latest developments and best practices in C#.NET development include: 1. Asynchronous programming improves application responsiveness, and simplifies non-blocking code using async and await keywords; 2. LINQ provides powerful query functions, efficiently manipulating data through delayed execution and expression trees; 3. Performance optimization suggestions include using asynchronous programming, optimizing LINQ queries, rationally managing memory, improving code readability and maintenance, and writing unit tests.

C# .NET: Building Applications with the .NET EcosystemC# .NET: Building Applications with the .NET EcosystemApr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# as a Versatile .NET Language: Applications and ExamplesC# as a Versatile .NET Language: Applications and ExamplesApr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# .NET for Web, Desktop, and Mobile DevelopmentC# .NET for Web, Desktop, and Mobile DevelopmentApr 25, 2025 am 12:01 AM

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

C# .NET Ecosystem: Frameworks, Libraries, and ToolsC# .NET Ecosystem: Frameworks, Libraries, and ToolsApr 24, 2025 am 12:02 AM

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideDeploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideApr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

See all articles

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor