


A small example of implementing verification code and refreshing verification code in ASP.NET
Implementation code
/// <summary> /// 生成验证码图片,保存session名称VerificationCode /// </summary> public static void CreateVerificationCode() { int number; string checkCode = string.Empty; //随机数种子 Random randoms = new Random(); for (int i = 0; i < 4; i++) //校验码长度为4 { //随机的整数 number = randoms.Next(); //字符从0-9,A-Z中随机产生,对应的ASCII码分别为 //48-57,65-90 number = number % 36; if (number < 10) { number += 48; } else { number += 55; } checkCode += ((char)number).ToString(); } //在session中保存校验码 System.Web.HttpContext.Current.Session["VerificationCode"] = checkCode; //若校验码为空,则直接返回 if (checkCode == null || checkCode.Trim() == String.Empty) { return; } //根据校验码的长度确定输出图片的长度 System.Drawing.Bitmap image = new System.Drawing.Bitmap(55, 20);//(int)Math.Ceiling(Convert.ToDouble(checkCode.Length * 15)) //创建Graphics对象 Graphics g = Graphics.FromImage(image); try { //生成随机数种子 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的背景噪音线 10条 //--------------------------------------------------- for (int i = 0; i < 10; i++) { //噪音线起点坐标(x1,y1),终点坐标(x2,y2) 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.Silver), x1, y1, x2, y2); } //--------------------------------------------------- //Brush b = Brushes.Silver; //g.FillRectangle(b, 0, 0, image.Width, image.Height); //---------------------以上两种任选其一------------------------------ //输出图片中校验码的字体: 12号Arial,粗斜体 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); //线性渐变画刷 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Purple, 1.2f, true); g.DrawString(checkCode, font, brush, 2, 2); //画图片的前景噪音点 50个 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.Peru), 0, 0, image.Width - 1, image.Height - 1); //创建内存流用于输出图片 using (MemoryStream ms = new MemoryStream()) { //图片格式指定为png image.Save(ms, ImageFormat.Jpeg); //清除缓冲区流中的所有输出 System.Web.HttpContext.Current.Response.ClearContent(); //输出流的HTTP MIME类型设置为"image/Png" System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg"; //输出图片的二进制流 System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); } } finally { //释放Bitmap对象和Graphics对象 g.Dispose(); image.Dispose(); } }
Create an aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %> <%Help.CreateVerificationCode(); %>
Add HTML code, quote
<div class="positionR"> <label>验证码:</label> <span class="style1"> *</span> <input type="text" class="yanZm" runat="Server" reg="^.+$" id="txtAuthCode" tip="请输入验证码!" /> <img class="yanZm_img lazy" src="/static/imghwm/default1.png" data-src="AuthCode.aspx" alt="" id="imgAuthCode" /> </div>
How to achieve refresh?
<script type="text/javascript"> $("#imgAuthCode").click(function () { $(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime()); }); </script>
Rendering
For more ASP.NET small examples of implementing verification codes and refreshing verification codes, please pay attention to the PHP Chinese website!

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.

The future trends of C#.NET are mainly focused on three aspects: cloud computing, microservices, AI and machine learning integration, and cross-platform development. 1) Cloud computing and microservices: C#.NET optimizes cloud environment performance through the Azure platform and supports the construction of an efficient microservice architecture. 2) Integration of AI and machine learning: With the help of the ML.NET library, C# developers can embed machine learning models in their applications to promote the development of intelligent applications. 3) Cross-platform development: Through .NETCore and .NET5, C# applications can run on Windows, Linux and macOS, expanding the deployment scope.

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.

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# 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# 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.

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.

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.


Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
