小課堂:驗證碼的作用:
幾年前,大部分網站、論壇之類的是沒有驗證碼的,因為對於一般使用者來說驗證碼只是增加了使用者的操作,降低了使用者的體驗。但是後來各種灌水機器人、投票機器人、惡意註冊機器人層出不窮,大大增加了網站的負擔同時也給網站資料庫帶來了大量的垃圾資料。為了防止各種機器人程式的破壞,於是程式員想出了只有人眼能夠識別的,程式不容易識別的驗證碼!
驗證碼是一個圖片,將字母、數字甚至漢字作為圖片的內容,這樣一張圖片中的內容用人眼很容易識別,而程式將無法識別。在進行資料庫操作之前(比如登入驗證、投票、發帖、回複、註冊等等)程式首先驗證用戶端提交的驗證碼是否與圖片中的內容相同,如果相同則進行資料庫操作,不同則提示驗證碼錯誤,不進行資料庫操作。這樣各種機器人程式就被拒之門外了!
但是隨著電腦科學的發展,模式識別等技術越來越成熟,於是編寫機器人程式的傢伙可以通過程式將直接寫在圖片中的內容識別出來,然後提交到伺服器,這樣驗證碼將形同虛設。為了防止機器人程式的識別,驗證碼的圖片產生也不斷在發展,加入幹擾點、幹擾線,文字變形、變換角度位置,顏色不同……各種防止電腦識別的技術也應用到驗證碼中。就在這兩種技術的競爭中,於是便形成了我們現在看到的驗證碼,已經有很多人在抱怨“這是什麼驗證碼哦,人眼都分辨不清楚是什麼”,一切也是無奈。
瞭解了驗證碼的作用,下面寫一個簡單的驗證碼產生及使用的執行個體
先建個頁面用來展示驗證碼和判斷驗證碼輸入是否正確
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript"> //點擊切換驗證碼 function f_refreshtype() { var Image1 = document.getElementById("img"); if (Image1 != null) { Image1.src = Image1.src + "?"; } } </script></head><body> <form id="form1" runat="server"> <div> <table> <tr> <td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </td> <td> <img src="png.aspx" id="img" onclick="f_refreshtype()" /> </td> <td> <asp:Button ID="Button1" runat="server" Text="確定" /> </td> </tr> </table> </div> </form></body></html>
此頁面後台對驗證碼進行驗證
protected void Page_Load(object sender, EventArgs e){//產生的驗證碼被儲存到session中if (Session["CheckCode"] != null){string checkcode = Session["CheckCode"].ToString();if (this.TextBox1.Text == checkcode){ ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('驗證碼輸入正確!')", true);}else{ ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('驗證碼輸入錯誤!')", true);}} }
產生驗證碼頁面png.aspx
protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){CreateCheckCodeImage(GenerateCheckCodes(4));}}public void ShowAuthCode(Stream stream, out string code){Random random = new Random();code = random.Next(1000, 9999).ToString(); Bitmap bitmap = CreateAuthCode(code);bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);} private string GenerateCheckCodes(int iCount){int number;string checkCode = String.Empty;int iSeed = DateTime.Now.Millisecond;System.Random random = new Random(iSeed);for (int i = 0; i < iCount; i++){number = random.Next(10);checkCode += number.ToString();}Session["CheckCode"] = checkCode;return checkCode;} private Bitmap CreateAuthCode(string str){Font fn = new Font("宋體", 12);Brush forecolor = Brushes.Black;Brush bgcolor = Brushes.White;PointF pf = new PointF(5, 5);Bitmap bitmap = new Bitmap(100, 25);Rectangle rec = new Rectangle(0, 0, 100, 25);Graphics gh = Graphics.FromImage(bitmap);gh.FillRectangle(bgcolor, rec);gh.DrawString(str, fn, forecolor, pf);return bitmap;} private void CreateCheckCodeImage(string checkCode){if (checkCode == null || checkCode.Trim() == String.Empty)return;int iWordWidth = 15;int iImageWidth = checkCode.Length * iWordWidth;Bitmap image = new Bitmap(iImageWidth, 20);Graphics g = Graphics.FromImage(image);try{//產生隨機產生器 Random random = new Random();//清空圖片背景色 g.Clear(Color.White); //畫圖片的背景雜音點for (int i = 0; i < 20; 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);} //畫圖片的背景雜音線 for (int i = 0; i < 2; i++){ int x1 = 0; int x2 = image.Width; int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); if (i == 0) { g.DrawLine(new Pen(Color.Gray, 2), x1, y1, x2, y2); } } for (int i = 0; i < checkCode.Length; i++){ string Code = checkCode[i].ToString(); int xLeft = iWordWidth * (i); random = new Random(xLeft); int iSeed = DateTime.Now.Millisecond; int iValue = random.Next(iSeed) % 4; if (iValue == 0) { Font font = new Font("Arial", 13, (FontStyle.Bold | System.Drawing.FontStyle.Italic)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Red, 1.5f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 1) { Font font = new System.Drawing.Font("楷體", 13, (FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.DarkRed, 1.3f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 2) { Font font = new System.Drawing.Font("宋體", 13, (System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Green, Color.Blue, 1.2f, true); g.DrawString(Code, font, brush, xLeft, 2); } else if (iValue == 3) { Font font = new System.Drawing.Font("黑體", 13, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Bold)); Rectangle rc = new Rectangle(xLeft, 0, iWordWidth, image.Height); LinearGradientBrush brush = new LinearGradientBrush(rc, Color.Blue, Color.Green, 1.8f, true); g.DrawString(Code, font, brush, xLeft, 2); }}//////畫圖片的前景噪音點 //for (int i = 0; i < 8; 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);System.IO.MemoryStream ms = new System.IO.MemoryStream();image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);Response.ClearContent();Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}