ASP.NET實作驗證碼以及刷新驗證碼的小例子
實作程式碼
/// <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(); } }
建立一個aspx頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AuthCode.aspx.cs" Inherits="AuthCode" %> <%Help.CreateVerificationCode(); %>
加入HTML程式碼,引用
<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/imghw/default1.png" data-src="AuthCode.aspx" alt="" id="imgAuthCode" /> </div>
如何實作刷新?
<script type="text/javascript"> $("#imgAuthCode").click(function () { $(this).attr("src", "AuthCode.aspx?code=" + (new Date()).getTime()); }); </script>
效果圖
NET碼

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。
