


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!

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.

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.

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

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.


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

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

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

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.

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),