In fact, there are already many articles about the production of asp.net verification code, but today I still want to share it with you. Dear, you can combine several examples to write an ASP.NET verification code suitable for your own website, probably There are only two parts:
First create an asp.net form ValidateCode.aspx; do not write anything. Write the following code directly in the background ValidateCode.aspx.cs:
protected void Page_Load(object sender, EventArgs e) { string validateCode = CreateValidateCode();//生成驗(yàn)證碼 Bitmap bitmap = new Bitmap(imgWidth,imgHeight);//生成Bitmap圖像 DisturbBitmap(bitmap); //圖像背景 DrewValidateCode(bitmap,validateCode);//繪制驗(yàn)證碼圖像 bitmap.Save(Response.OutputStream,ImageFormat.Gif);//保存圖像,等待輸出 } private int codeLen = 4;//驗(yàn)證碼長(zhǎng)度 private int fineness = 85;//圖片清晰度 private int imgWidth = 48;//圖片寬度 private int imgHeight = 24;//圖片高度 private string fontFamily = "Times New Roman";//字體名稱 private int fontSize = 14;//字體大小 //private int fontStyle = 0;//字體樣式 private int posX = 0;//繪制起始坐標(biāo)X private int posY = 0;//繪制坐標(biāo)Y private string CreateValidateCode() //生成驗(yàn)證碼 { string validateCode = ""; Random random = new Random();// 隨機(jī)數(shù)對(duì)象 for (int i = 0; i < codeLen; i++)//循環(huán)生成每位數(shù)值 { int n = random.Next(10);//數(shù)字 validateCode += n.ToString(); } Session["vcode"] = validateCode;//保存驗(yàn)證碼 這Session是在前臺(tái)調(diào)用的。 return validateCode;// 返回驗(yàn)證碼 } private void DisturbBitmap(Bitmap bitmap)//圖像背景 { Random random = new Random();//通過隨機(jī)數(shù)生成 for (int i = 0; i < bitmap.Width; i++)//通過循環(huán)嵌套,逐個(gè)像素點(diǎn)生成 { for (int j = 0; j < bitmap.Height; j++) { if (random.Next(90) <= this.fineness) bitmap.SetPixel(i, j, Color.LightGray); } } } private void DrewValidateCode(Bitmap bitmap, string validateCode)//繪制驗(yàn)證碼圖像 { Graphics g = Graphics.FromImage(bitmap);//獲取繪制器對(duì)象 Font font = new Font(fontFamily, fontSize, FontStyle.Bold);//設(shè)置繪制字體 g.DrawString(validateCode, font, Brushes.Black, posX, posY);//繪制驗(yàn)證碼圖像 }
Login.aspx form front desk:
//這個(gè)函數(shù)是在點(diǎn)擊驗(yàn)證碼圖片就會(huì)更換驗(yàn)證碼 //可以使用微軟自帶的jqury.js 下面jquery-1.4.1.min.js版本之上的?;蛘咴趈query官網(wǎng)上下載就可以。 <script src="styles/jquery-1.4.1.min.js" type="text/javascript"></script> function f_refreshtype() { var Image1 = document.getElementByIdx_x_x_x("img"); if (Image1 != null) { Image1.src = Image1.src + "?"; } } ---<img src="ValidateCode.aspx" id="img" onclick="f_refreshtype()" width="50px"/>//調(diào)用函數(shù),實(shí)現(xiàn)更換驗(yàn)證碼
Backend code: Click login to verify whether the user input is correct .
string usercode = txtcode.Text.Trim(); if (usercode == Session["vcode"].ToString())//Session["vcode"] { }
Other codes are the same as others.
The above is the process of generating ASP.NET verification code shared with you. I hope you can apply what you have learned.
For more articles related to the simple production of asp.net verification codes, 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

TheyieldkeywordinC#simplifiesiteratorcreationbyautomaticallygeneratingastatemachinethatenableslazyevaluation.1.Itallowsreturningitemsoneatatimeusingyieldreturn,pausingexecutionbetweeneachitem,whichisidealforlargeordynamicsequences.2.yieldbreakcanbeus

DependencyInjection(DI)inC#isadesignpatternthatenhancesmodularity,testability,andmaintainabilitybyallowingclassestoreceivedependenciesexternally.1.DIpromotesloosecouplingbydecouplingobjectcreationfromusage.2.Itsimplifiestestingthroughmockobjectinject

The role of IDisposable and using in C# is to efficiently and deterministically manage unmanaged resources. 1. IDisposable provides Dispose() method, so that the class can clearly define how to release unmanaged resources; 2. The using statement ensures that Dispose() is automatically called when the object is out of scope, simplifying resource management and avoiding leakage; 3. When using it, please note that the object must implement IDisposable, can declare multiple objects, and should always use using for types such as StreamReader; 4. Common best practices include not relying on destructors to clean up, correctly handling nested objects, and implementing the Dispose(bool) pattern.

LambdaexpressionsandLINQsimplifydatamanipulationinC#byenablingconcise,readable,andefficientcode.1.Lambdaexpressionsallowinlinefunctiondefinitions,makingiteasiertopasslogicasargumentsforfiltering,transforming,sorting,andaggregatingdatadirectlywithinme

Nullablereferencetypes(NRTs)inC#8 helpcatchNullReferenceExceptionerrorsatcompiletimebymakingreferencetypesnon-nullablebydefault,requiringexplicitdeclarationfornullability.NRTsmustbeenabledeitherinthe.csprojfilewithenableoratthetopofa.csfileusing#null

Four common "anti-pattern" problems in C# development need to be avoided. First, the unreasonable use of async/await leads to deadlocks or performance degradation. We should adhere to the principle of full asynchronousness, configure ConfigureAwait(false) and standardize naming; second, excessive dependence on var affects readability, and explicitly declare and unify team specifications when the type is unclear; third, the incorrect use of Dispose and resource management causes leakage, and the use statement should be used correctly and the IDisposable standard mode should be implemented; fourth, the abuse of static classes or singletons causes testing difficulties, and priority should be given to dependency injection, statelessness, or the life cycle managed by containers. Avoiding these misunderstandings can significantly improve code quality and maintenance.

Span and Memory improve C# performance by reducing memory allocation. 1. Span avoids array copying and provides light references to existing memory, which is suitable for parsing binary protocols, string operations and high-performance buffer management; 2. Memory supports passing memory slices across asynchronous methods, which is suitable for scenarios where more flexible life cycles are required; 3. Both reduce GC pressure, optimize performance by reusing buffers and avoiding temporary copying; 4. Span is limited to use on the stack and cannot be stored in classes or used in asynchronous methods. Be careful to avoid reassignment operations such as calling.ToArray().

SOLID principle is five design principles to improve code maintainability and scalability in object-oriented programming. They are: 1. The single responsibility principle (SRP) requires that the class only assumes one responsibility, such as separating report generation and email sending; 2. The opening and closing principle (OCP) emphasizes that the extension is supported through interfaces or abstract classes without modifying the original code, such as using the IShape interface to realize area calculation of different graphics; 3. The Richter replacement principle (LSP) requires that the subclass can replace the parent class without destroying logic, such as Square should not mistakenly inherit Rectangle, resulting in abnormal behavior; 4. The interface isolation principle (ISP) advocates the definition of fine-grained interfaces, such as split printing and scanning functions to avoid redundant dependencies; 5. The dependency inversion principle (DIP) advocates the
