ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能示例【附demo源碼】
本文實(shí)例講述了ASP.NET實(shí)現(xiàn)的生成驗(yàn)證碼功能。分享給大家供大家參考,具體如下:
生成驗(yàn)證碼原理:產(chǎn)生隨機(jī)字符,并將字符生成為圖片,同時(shí)儲(chǔ)存到Session里去,然后驗(yàn)證用戶輸入的內(nèi)容是否與Session中的驗(yàn)證碼相符即可。
效果圖:用戶可以點(diǎn)擊切換驗(yàn)證碼信息。
一般處理程序:CheckCodeHandler.cs
<%@ WebHandler Language="C#" Class="CheckCodeHandler" %> using System; using System.Web; using System.Text; using System.Drawing; using System.Web.SessionState; public class CheckCodeHandler : IHttpHandler,IRequiresSessionState { //產(chǎn)生驗(yàn)證碼的字符集 public string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y"; public void ProcessRequest (HttpContext context) { string validateCode = CreateRandomCode(4); context.Session["ValidateCode"] = validateCode;//將驗(yàn)證碼保存到session中 CreateCodeImage(validateCode, context); } public bool IsReusable { get { return false; } } /// <summary> /// 生成驗(yàn)證碼 /// </summary> /// <param name="n">驗(yàn)證碼個(gè)數(shù)</param> /// <returns>驗(yàn)證碼字符串</returns> public string CreateRandomCode(int n) { string[] CharArray = charcode.Split(',');//將字符串轉(zhuǎn)換為字符數(shù)組 string randomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < n; i++) { if (temp != -1) { rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = rand.Next(CharArray.Length - 1); if (temp != -1 && temp == t) { return CreateRandomCode(n); } temp = t; randomCode += CharArray[t]; } return randomCode; } public void CreateCodeImage(string checkCode, HttpContext context) { int iwidth = (int)(checkCode.Length * 13); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Italic | System.Drawing.FontStyle.Bold)); // 前景色 Brush b = new System.Drawing.SolidBrush(Color.Black); // 背景色 g.Clear(Color.White); // 填充文字 g.DrawString(checkCode, f, b, 0, 1); // 隨機(jī)線條 Pen linePen = new Pen(Color.Gray, 0); Random rand = new Random(); for (int i = 0; i < 5; i++) { int x1 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int x2 = rand.Next(image.Width); int y2 = rand.Next(image.Height); g.DrawLine(linePen, x1, y1, x2, y2); } // 隨機(jī)點(diǎn) for (int i = 0; i < 30; i++) { int x = rand.Next(image.Width); int y = rand.Next(image.Height); image.SetPixel(x, y, Color.Gray); } // 邊框 g.DrawRectangle(new Pen(Color.Gray), 0, 0, image.Width - 1, image.Height - 1); // 輸出圖片 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.ClearContent(); context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } }
封裝成類庫:ValidateNumber.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; using System.Web.UI; using System.Drawing.Drawing2D; using System.IO; using System.Drawing.Imaging; /// <summary> ///ValidateNumber 生成驗(yàn)證碼 /// </summary> public class ValidateNumber { //產(chǎn)生驗(yàn)證碼的字符集 (易混淆的字符去掉) private string charcode = "2,3,4,5,6,8,9,A,B,C,D,E,F,G,H,J,K,M,N,P,R,S,U,W,X,Y,a,b,c,d,e,f,g,h,j,k,m,n,p,r,s,u,w,x,y"; /// <summary> /// 驗(yàn)證碼的最大長(zhǎng)度 /// </summary> public int MaxLength { get { return 10; } } /// <summary> /// 驗(yàn)證碼的最小長(zhǎng)度 /// </summary> public int MinLength { get { return 1; } } /// <summary> /// 生成驗(yàn)證碼 /// </summary> /// <param name="length">指定驗(yàn)證碼的長(zhǎng)度</param> /// <returns></returns> public string CreateValidateNumber(int length) { string[] CharArray = charcode.Split(',');//將字符串轉(zhuǎn)換為字符數(shù)組 string randomCode = ""; int temp = -1; Random rand = new Random(); for (int i = 0; i < length; i++) { if (temp != -1) { rand = new Random(i * temp * ((int)DateTime.Now.Ticks)); } int t = rand.Next(CharArray.Length - 1); if (temp != -1 && temp == t) { return CreateValidateNumber(length); } temp = t; randomCode += CharArray[t]; } return randomCode; } /// <summary> /// 創(chuàng)建驗(yàn)證碼的圖片 /// </summary> /// <param name="context">context對(duì)象</param> /// <param name="validateNum">驗(yàn)證碼</param> public void CreateValidateGraphic(HttpContext context,string validateNum) { int iwidth = (int)(validateNum.Length * 14); Bitmap image = new Bitmap(iwidth, 22); Graphics g = Graphics.FromImage(image); try { //生成隨機(jī)生成器 Random random = new Random(); //清空?qǐng)D片背景色 g.Clear(Color.White); //畫圖片的干擾線 for (int i = 0; i < 25; 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.Silver), x1, y1, x2, y2); } 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.DarkRed, 1.2f, true); g.DrawString(validateNum, font, brush, 3, 2); //畫圖片的前景干擾點(diǎn) for (int i = 0; i < 100; 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); //保存圖片數(shù)據(jù) MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); //輸出圖片 context.Response.Clear(); context.Response.ContentType = "image/jpeg"; context.Response.BinaryWrite(stream.ToArray()); } finally { g.Dispose(); image.Dispose(); } } /// <summary> /// 得到驗(yàn)證碼圖片的長(zhǎng)度 /// </summary> /// <param name="validateNumLength">驗(yàn)證碼的長(zhǎng)度</param> /// <returns></returns> public static int GetImageWidth(int validateNumLength) { return (int)(validateNumLength * 14); } /// <summary> /// 得到驗(yàn)證碼圖片的高度 /// </summary> /// <returns></returns> public static double GetImageHeight() { return 22; } }
附:完整實(shí)例代碼點(diǎn)擊此處本站下載。
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
- asp.net 驗(yàn)證碼生成和刷新及驗(yàn)證
- asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
- asp.net 簡(jiǎn)單驗(yàn)證碼驗(yàn)證實(shí)現(xiàn)代碼
- ASP.NET MVC驗(yàn)證碼功能實(shí)現(xiàn)代碼
- ASP.NET中的無刷新驗(yàn)證碼的開發(fā)(完整代碼)
- asp.net 圖片驗(yàn)證碼的HtmlHelper
- asp.net生成驗(yàn)證碼(純數(shù)字)
- asp.net中3種驗(yàn)證碼示例(實(shí)現(xiàn)代碼)(數(shù)字,數(shù)字字母混和,漢字)
- asp.net 驗(yàn)證碼的簡(jiǎn)單制作(vb.net+C#)
- asp.net下生成英文字符數(shù)字驗(yàn)證碼的代碼
- asp.net生成字母和數(shù)字混合圖形驗(yàn)證碼
- Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
相關(guān)文章
詳解如何在ASP.NET Core Web API中以三種方式返回?cái)?shù)據(jù)
這篇文章主要介紹了詳解如何在ASP.NET Core Web API中以三種方式返回?cái)?shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01asp.net 修改/刪除站內(nèi)目錄操作后Session丟失問題
在Web項(xiàng)目中使用 Directory.Move(olddir,newdir)修改目錄名稱或使用Directory.Delete(true)刪除目錄后, 發(fā)現(xiàn)Session都失效。2010-01-01ASP.NET數(shù)據(jù)綁定GridView控件使用技巧
這篇文章主要為大家詳細(xì)介紹了ASP.NET數(shù)據(jù)綁定GridView控件使用技巧,感興趣的小伙伴們可以參考一下2016-03-03ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證,感興趣的小伙伴們可以參考一下2016-07-07實(shí)現(xiàn)onmouseover和onmouseout應(yīng)用于RadioButtonList或CheckBoxList控件上
一直想實(shí)現(xiàn)onmouseover和onmouseout應(yīng)用于RadioButtonList或CheckBoxList控件上。此功能就是當(dāng)鼠標(biāo)經(jīng)過時(shí)RadioButtonList或CheckBoxList每一個(gè)Item時(shí),讓Item有特效顯示,離開時(shí),恢復(fù)原樣有演示動(dòng)畫,感興趣的朋友可以了解下啊2013-01-01asp.net基礎(chǔ)學(xué)習(xí)之控件的使用方法
這篇文章主要為大家詳細(xì)介紹了asp.net基礎(chǔ)學(xué)習(xí)之控件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08