javaScript實現動態取得不同的驗證碼
阿新 • • 發佈:2019-02-02
一般我們登入系統、應用或是網站的時候,除了需要輸入使用者名稱和密碼之外,有些還需要輸入驗證碼,就像剛剛登陸的這個網站一樣:
驗證碼那麼簡單為什麼還需要手動輸入呢?只要是認得10個數字和26個英文字母的人都可以正確輸入,當然也有圖片和中文的驗證碼。就像12306火車票網上訂票系統一樣,每次登陸都需要進行圖文驗證。百度百科中對其定義是:驗證碼(CAPTCHA)是“Completely
Automated Public Turingtest to tell Computers and Humans Apart ”(全自動區分計算機和人類的圖靈測試)的縮寫,是一種區分使用者是計算機還是人的公共全自動程式。可以知道這是一個很智慧的東西,可以防止:惡意破解密碼、刷票、論壇灌水等,所以現在可以理解12306網上訂票系統的圖文驗證怎麼那麼複雜了,一般登陸的話總得看半天才知道那字、那圖是什麼。接下來看看.net中是怎麼三步簡單實現驗證碼登陸,注:主要是根據牛腩老師介紹的新聞釋出系統進行實踐的。
一:新建一個“handler”資料夾,用來存放“一般處理程式”——WaterMark.ashx
using System; using System.Web; using System.Drawing; using System.Drawing.Drawing2D; using System.Web.SessionState; public class WaterMark : IHttpHandler, IRequiresSessionState { // 要使用session必須實現該介面需要匯入System.Web.SessionState名稱空間 public void ProcessRequest(HttpContext context) { string checkCode = GenCode(5); // 產生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(); } } /// <summary> /// 產生隨機字串 /// </summary> /// <param name="num">隨機出幾個字元</param> /// <returns>隨機出的字串</returns> private string GenCode(int num) { //根據需要產生的字元進行設定 string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] chastr = str.ToCharArray(); string code = ""; Random rd = new Random(); int i; for (i = 0; i < num; i++) { code += str.Substring(rd.Next(0, str.Length), 1); } return code; } public bool IsReusable { get { return false; } } }
二、介面上的驗證碼用img來檢視
img中的字串用WaterMark.ashx產生的隨機字串,也就是<img src="handler/WaterMark.ashx" id="vimg"alt="" onclick ="changeCode()"/>
ps:img的路徑一定要注意
要實現點選img驗證碼可以實時切換,可以使用javaScript來實現,比較簡單,可以直接巢狀在介面上的程式碼裡:
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> <script language ="javascript" type ="text/javascript" > function changeCode() { var imgNode = document.getElementById("vimg"); imgNode.src = "handler/WaterMark.ashx?t=" + (new Date()).valueOf();} </script> </asp:Content>
最後就可以實現單擊切換驗證碼的功能啦,效果顯示: