asp.net使用ashx生成圖形驗證碼的方法示例
本文實例講述了asp.net使用ashx生成圖形驗證碼的方法。分享給大家供大家參考,具體如下:
驗證碼的好處不用我多說,你們都懂的。我在網(wǎng)上看到有人把驗證碼直接寫在aspx頁面里,也就是說這種方式請求驗證碼等于請求一個頁面,這樣做很不科學(xué)。如下所示
<form id="form1" runat="server"> <div> <asp:Image ID="Image1" runat="server" ImageUrl="Default.aspx" /> <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </div> </form>
這個代碼看著就覺得寫代碼的人比較欠揍,代碼寫成這樣子著實郁悶。驗證嗎也不寫點script做下切換。
下面我介紹一種方式來實現(xiàn)這樣的功能
1. 寫個ashx生成圖形驗證碼
using System; using System.Collections; using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.Web.SessionState; using System.Drawing; namespace usechecknum.ashx { /// <summary> /// $codebehindclassname$ 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class doCreateNum : IHttpHandler,IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string checkCode = GetValidation(5); // 產(chǎn)生5位隨機驗證碼字符 context.Session["Code"] = checkCode; //將字符串保存到Session中,以便需要時進行驗證 System.Drawing.Bitmap image = new System.Drawing.Bitmap(70, 22); Graphics g = Graphics.FromImage(image); try { //生成隨機生成器 Random random = new Random(); //清空圖片背景色 g.Clear(Color.White); // 畫圖片的背景噪音線 int i; for (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 System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2F, true); g.DrawString(checkCode, font, brush, 2, 2); //畫圖片的前景噪音點 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; context.Response.BinaryWrite(ms.ToArray()); } finally { g.Dispose(); image.Dispose(); } } public string GetValidation(int num) { string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //"或者寫漢字也行" string validatecode = ""; Random rd = new Random(); for (int i = 0; i < num; i++) { validatecode += str.Substring(rd.Next(0, str.Length), 1); } return validatecode; } public bool IsReusable { get { return false; } } } }
2. 在頁面上顯示驗證碼,因為我們生成的是圖形,所以可以直接寫在<img/>標簽里,我們只要寫段簡單的腳本就可以點擊鼠標切換驗證碼了
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="usechecknum._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>驗證碼的使用</title> </head> <script language="javascript" type="text/javascript"> function changeCode() { var imgNode = document.getElementById("vimg"); imgNode.src = "ashx/doCreateNum.ashx?t=" + (new Date()).valueOf(); // 這里加個時間的參數(shù)是為了防止瀏覽器緩存的問題 } </script> <body> <form id="form1" runat="server"> 請輸入驗證碼:<input type="text" name="checknum"/><img src="ashx/doCreateNum.ashx" id="vimg" onclick="changeCode()" /> </form> </body> </html>
說了半天,該是時候看看生成的驗證碼長什么樣了
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net操作json技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對大家asp.net程序設(shè)計有所幫助。
相關(guān)文章
Asp.net內(nèi)置對象之Server對象(概述及應(yīng)用)
Server對象提供對服務(wù)器上的方法和屬性的訪問以及進行HTML編碼的功能,本文主要圍繞server對象介紹詳細功能及常用屬性和主要方法,感興趣的朋友可以了解下,或許對你學(xué)習(xí)server對象有所幫助2013-02-02blog程序新版本V2.0 Beta完成,提供V1.0全部源碼下載
blog程序新版本V2.0 Beta完成,提供V1.0全部源碼下載...2007-06-06ASP.NET?Core使用MiniProfiler分析應(yīng)用
這篇文章介紹了ASP.NET?Core使用MiniProfiler分析應(yīng)用的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
這篇文章主要介紹了ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄)
Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄),需要的朋友可以參考下2008-07-07