1、驗證碼簡介
驗證碼功能通常是用於防止批量註冊的,很多網站爲了防止用戶利用機器人自動註冊、登陸、灌水,都採用了驗證碼技術。所謂驗證碼,就是將一串隨機產生的數字或字母或符號或文字,生成一幅圖片, 圖片里加上一些干擾象素(防止OCR),由用戶肉眼識別其中的驗證碼信息,輸入表單提交網站驗證,驗證成功後才能使用某項功能。 dom
常見的驗證碼有以下幾種:
一、純數字驗證碼,通常爲四位隨機數字;
二、數字+字母驗證碼,通常從數字(0~9)和字母(A~Z和a~z)中隨機抽出幾個字符組成;
三、漢字驗證碼,相對而言,這種驗證碼比較少見一點,實現起來也相對複雜一些,但在很多網站中仍是能夠看到的; 網站
2、驗證碼的實現
一、純數字驗證碼的實現
純數字驗證碼的實現相對比較簡單,可經過如下兩種方法來實現
ui
/// <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(); }
二、數字與字母組合字符串spa
字母與數字組合的字符串也比較簡單 能夠按照本身的規則去生成大小寫字母code
/// <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(); }
三、生成圖片流orm
原理:先生成驗證碼,而後把生成的驗證碼轉化爲圖片流進行輸出blog
///<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()); } }