產生驗證碼
首先我們必須先撰寫一支產生驗證碼圖片的程式,程式碼主要是是透過Bitmap物件來幫我們產生圖檔(想成一張空白畫布),然後在畫布上畫上我們要的內容(可以是任意內容)。
01 | public void VerificationCode() |
07 | if (Session[ "CreateTime" ] == null ) |
09 | Session[ "CreateTime" ] = DateTime.Now; |
13 | DateTime startTime = Convert.ToDateTime(Session[ "CreateTime" ]); |
14 | DateTime endTime = Convert.ToDateTime(DateTime.Now); |
15 | TimeSpan ts = endTime - startTime; |
22 | Session[ "CreateTime" ] = DateTime.Now; |
30 | Response.ContentType = "image/gif" ; |
32 | Bitmap basemap = new Bitmap(200, 60); |
33 | Graphics graph = Graphics.FromImage(basemap); |
34 | graph.FillRectangle( new SolidBrush(Color.White), 0, 0, 200, 60); |
35 | Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel); |
36 | Random random = new Random(); |
40 | string letters = "甲乙丙丁戊己庚辛壬癸子丑寅卯辰巳午未申酉戍亥鼠牛虎免龍蛇馬羊猴雞狗豬" ; |
42 | StringBuilder sb = new StringBuilder(); |
49 | for ( int word = 0; word < 2; word++) |
51 | letter = letters.Substring(random.Next(0, letters.Length - 1), 1); |
56 | graph.DrawString(letter, font, new SolidBrush(Color.Black), word * 38, random.Next(0, 15)); |
62 | string currentCode = Session[ "ValidateCode" ].ToString(); |
63 | sb.Append(currentCode); |
65 | foreach ( char item in currentCode) |
67 | letter = item.ToString(); |
69 | graph.DrawString(letter, font, new SolidBrush(Color.Black), currentCode.IndexOf(item) * 38, random.Next(0, 15)); |
75 | Pen linePen = new Pen( new SolidBrush(Color.Black), 2); |
76 | for ( int x = 0; x < 10; x++) |
78 | graph.DrawLine(linePen, new Point(random.Next(0, 199), random.Next(0, 59)), new Point(random.Next(0, 199), random.Next(0, 59))); |
82 | basemap.Save(Response.OutputStream, ImageFormat.Gif); |
84 | Session[ "ValidateCode" ] = sb.ToString(); |
內容想呈現中文或英文選擇letters變數的內容即可。
表單使用CAPTCHA
要在MVC的View Page去取得我們的CAPTCHA驗證碼圖也很簡單,只需要在<img src="圖片路徑">
即可。
Controller
1 | public ActionResult Index() |
3 | Session[ "User" ] = "Bruce" ; |
在Index裡為何要寫那行Session呢?
03 | @using (Html.BeginForm("ValidateCode", "Captcha")) |
07 | < img src = "@Url.Action(" VerificationCode")" alt = "驗證碼" /> |
08 | @Html.TextBox("InputCode", null, new { placeholder="請輸入驗證碼" }) |
09 | < input type = "submit" value = "確定" /> |
15 | < h2 >Captcha測試(AJAX)</ h2 > |
18 | 驗證結果:< span id = "status" ></ span > |
21 | @using (Ajax.BeginForm("ValidateCode", "Captcha", new AjaxOptions() { UpdateTargetId = "status" })) |
25 | < img src = "@Url.Action(" VerificationCode")" alt = "驗證碼" /> |
26 | @Html.TextBox("InputCode", null, new { placeholder = "請輸入驗證碼" }) |
27 | < input type = "submit" value = "確定" /> |
33 | < script src = "~/Scripts/jquery.unobtrusive-ajax.min.js" ></ script > |
驗證碼的檢查也很合適使用AJAX來進行確認,各位可以測試一般表單和AJAX效果的表單。
驗證輸入
01 | public ActionResult ValidateCode( string InputCode) |
03 | if (InputCode == null ) |
04 | return Content( "輸入空白" ); |
06 | if (InputCode.Trim().ToLower().Equals(Session[ "ValidateCode" ].ToString().ToLower())) |
如果是選擇使用中文內容,可不需要加上.ToLower()
方法。
請問,我在驗證輸入時,Session["ValidateCode"] is null,需要跑第二次才會有值,一直不知道為什麼@@
回覆刪除您好,我是用 MVC5+ ASP.NET+C#,我用了程式碼後,發現驗證結果那一塊會有問題,無法顯示。
回覆刪除另外有沒有辦法結合 Ajax Reload 圖形???
您好,我是用 MVC5+ASP.NET+AJAX,使用程式碼後,發現 Ajax的測試結果會有問題,會無法顯示。
回覆刪除另外,有辦法結合 Ajax Reload 圖形嗎?
我寫了很多次都失敗ˊˋ