這篇文章給大家分享的是有關(guān)怎么使用ASP.NET實(shí)現(xiàn)生成驗證碼功能的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
十載的惠城網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整惠城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“惠城網(wǎng)站設(shè)計”,“惠城網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實(shí)執(zhí)行。
生成驗證碼原理:產(chǎn)生隨機(jī)字符,并將字符生成為圖片,同時儲存到Session里去,然后驗證用戶輸入的內(nèi)容是否與Session中的驗證碼相符即可。
一般處理程序: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)生驗證碼的字符集 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;//將驗證碼保存到session中 CreateCodeImage(validateCode, context); } public bool IsReusable { get { return false; } } ////// 生成驗證碼 /// /// 驗證碼個數(shù) ///驗證碼字符串 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; //////ValidateNumber 生成驗證碼 /// public class ValidateNumber { //產(chǎ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"; ////// 驗證碼的最大長度 /// public int MaxLength { get { return 10; } } ////// 驗證碼的最小長度 /// public int MinLength { get { return 1; } } ////// 生成驗證碼 /// /// 指定驗證碼的長度 ///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; } /// /// 創(chuàng)建驗證碼的圖片 /// /// context對象 /// 驗證碼 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(); //清空圖片背景色 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(); } } ////// 得到驗證碼圖片的長度 /// /// 驗證碼的長度 ///public static int GetImageWidth(int validateNumLength) { return (int)(validateNumLength * 14); } /// /// 得到驗證碼圖片的高度 /// ///public static double GetImageHeight() { return 22; } }
感謝各位的閱讀!關(guān)于怎么使用ASP.NET實(shí)現(xiàn)生成驗證碼功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!