一、驗證碼簡介 驗證碼功能一般是用于防止批量注冊的,不少網站為了防止用戶利用機器人自動注冊、登錄、灌水,都采用了驗證碼技術。所謂驗證碼,就是將一串隨機產生的數字或字母或符號或文字,生成一幅圖片, 圖片里加上一些干擾象素(防止OCR),由用戶肉眼識別其中的驗證碼信息,輸入表單提交網站驗證,驗證成功后才能使用某項功能。
常見的驗證碼有如下幾種: 1、純數字驗證碼,一般為四位隨機數字; 2、數字+字母驗證碼,一般從數字(0~9)和字母(A~Z和a~z)中隨機抽出幾個字符組成; 3、漢字驗證碼,相對而言,這種驗證碼比較少見一點,實現起來也相對復雜一些,但在不少網站中還是可以看到的;
二、驗證碼的實現 1、純數字驗證碼的實現 純數字驗證碼的實現相對比較簡單,可通過以下兩種方法來實現
/// <summary> /// 數字驗證碼 /// </summary> /// <param name="codeCount">驗證碼的位數‘n’</param> /// <returns>返回‘n’位驗證碼的字符串</returns> PRivate static String GetRandomint(int codeCount) { Random random = new Random(); StringBuilder sbmin = new StringBuilder(); StringBuilder sbmax = new StringBuilder(); for (int i = 0; i < codeCount; i++) { sbmin.Append("1"); sbmax.Append("9"); } return random.Next(Convert.ToInt32(sbmin.ToString()), Convert.ToInt32(sbmax.ToString())).ToString(); }
2、數字與字母組合字符串
字母與數字組合的字符串也比較簡單 可以按照自己的規則去生成大小寫字母
/// <summary> /// 生成驗證碼字符串 /// </summary> /// <param name="codeLen">驗證碼字符長度</param> /// <returns>返回驗證碼字符串</returns> private static string MakeCode(int codeLen) { if (codeLen < 1) { return string.Empty; } int number; StringBuilder sbCheckCode = new StringBuilder(); Random random = new Random(); for (int index = 0; index < codeLen; index++) { number = random.Next(); if (number % 2 == 0) { sbCheckCode.Append((char)('0' + (char)(number % 10))); //生成數字 } else { sbCheckCode.Append((char)('A' + (char)(number % 26))); //生成字母 } } return sbCheckCode.ToString(); }
3、生成圖片流
原理:先生成驗證碼,然后把生成的驗證碼轉化為圖片流進行輸出
///<summary> /// 獲取驗證碼圖片流 /// </summary> /// <param name="checkCode">驗證碼字符串</param> /// <returns>返回驗證碼圖片流</returns> public static MemoryStream CreateCodeImg(string checkCode) { if (string.IsNullOrEmpty(checkCode)) { return null; } Bitmap image = new Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22); Graphics graphic = Graphics.FromImage(image); try { Random random = new Random(); graphic.Clear(Color.White); int x1 = 0, y1 = 0, x2 = 0, y2 = 0; for (int index = 0; index < 25; index++) { x1 = random.Next(image.Width); x2 = random.Next(image.Width); y1 = random.Next(image.Height); y2 = random.Next(image.Height); graphic.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Red, Color.DarkRed, 1.2f, true); graphic.DrawString(checkCode, font, brush, 2, 2); int x = 0; int y = 0; //畫圖片的前景噪音點 for (int i = 0; i < 100; i++) { x = random.Next(image.Width); y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //畫圖片的邊框線 graphic.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //將圖片驗證碼保存為流Stream返回 System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); return ms; } finally { graphic.Dispose(); image.Dispose(); } }
調用方法
在HttpHander的ProcessRequest中進行調用:pupublic void ProcessRequest(HttpContext context){ string code = MakeCode(5); context.Response.ClearContent(); context.Response.ContentType = "image/Gif"; MemoryStream ms = CreateCodeImg(code); if (null != ms) { context.Response.BinaryWrite(ms.ToArray()); }}
新聞熱點
疑難解答