驗證方法:
public static string VerificationCodeCacheFormat="vcode_cache_{0}";
public IActionResult ValidateCode() { VerificationCodeServices _vierificationCodeServices = new VerificationCodeServices(); string code = ""; System.IO.MemoryStream ms = _vierificationCodeServices.Create(out code); code = code.ToLower();//驗證碼不分大小寫 //HttpContext.Session.SetString("SupportValidateCode", code); Response.Body.Dispose(); //ViewBag.code = code; var token = Guid.NewGuid().ToString(); ViewBag.token = token; var cacheKey = string.Format(VerificationCodeCacheFormat, token); _memoryCache.Set(cacheKey, code, new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(20))); CookieOptions options = new CookieOptions(); Response.Cookies.Append("validatecode", token); return File(ms.ToArray(), @"image/png"); } /// <summary> /// 驗證碼改為后台驗證 /// </summary> /// <param name="userToken"></param> /// <param name="userVerCode"></param> /// <returns></returns> public bool VerifyUserInputCode(string userToken, string userVerCode) { var cacheKey = string.Format(VerificationCodeCacheFormat, userToken.ToString()); var vCode = ""; if (!_memoryCache.TryGetValue(cacheKey, out vCode)) return false; if (vCode.ToLower() != userVerCode.ToLower()) return false; _memoryCache.Remove(cacheKey); return true; }
生成驗證碼及其圖片
using System; using System.Collections.Generic; using System.DrawingCore; using System.DrawingCore.Imaging; using System.IO; using System.Linq; using System.Threading.Tasks; namespace homepage.Classes { ///NuGet中引入第三方 ZKWeb.System.Drawing 3.0版本 /// <summary> /// 圖片驗證碼 /// </summary> public class VerificationCodeServices { /// <summary> /// 生成指定長度的隨機字符串 /// </summary> /// <param name="codeLength">字符串的長度</param> /// <returns>返回隨機數字符串</returns> private string RndomStr(int codeLength) { //組成字符串的字符集合 0-9數字、大小寫字母 string chars = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] charArray = chars.Split(new Char[] { ',' }); string code = ""; int temp = -1;//記錄上次隨機數值,盡量避避免生產幾個一樣的隨機數 Random rand = new Random(); //采用一個簡單的算法以保證生成隨機數的不同 for (int i = 1; i < codeLength + 1; i++) { if (temp != -1) { rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));//初始化隨機類 } int t = rand.Next(61); if (temp == t) { return RndomStr(codeLength);//如果獲取的隨機數重復,則遞歸調用 } temp = t;//把本次產生的隨機數記錄起來 code += charArray[t];//隨機數的位數加一 } return code; } /// <summary> /// 將生成的字符串寫入圖像文件 /// </summary> /// <param name="code">驗證碼字符串</param> /// <param name="length">生成位數(默認4位)</param> public MemoryStream Create(out string code, int length = 4) { code = RndomStr(length); Bitmap Img = null; Graphics graphics = null; MemoryStream ms = null; Random random = new Random(); //顏色集合 Color[] color = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple }; //字體集合 string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" }; //定義圖像的大小,生成圖像的實例 Img = new Bitmap((int)code.Length * 18, 32); graphics = Graphics.FromImage(Img);//從Img對象生成新的Graphics對象 graphics.Clear(Color.White);//背景設為白色 //在隨機位置畫背景點 for (int i = 0; i < 100; i++) { int x = random.Next(Img.Width); int y = random.Next(Img.Height); graphics.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1); } //驗證碼繪制在graphics中 for (int i = 0; i < code.Length; i++) { int colorIndex = random.Next(7);//隨機顏色索引值 int fontIndex = random.Next(4);//隨機字體索引值 Font font = new Font(fonts[fontIndex], 15, FontStyle.Bold);//字體 Brush brush = new SolidBrush(color[colorIndex]);//顏色 int y = 4; if ((i + 1) % 2 == 0)//控制驗證碼不在同一高度 { y = 2; } graphics.DrawString(code.Substring(i, 1), font, brush, 3 + (i * 12), y);//繪制一個驗證字符 } ms = new MemoryStream();//生成內存流對象 Img.Save(ms, ImageFormat.Png);//將此圖像以Png圖像文件的格式保存到流中 graphics.Dispose(); Img.Dispose(); return ms; } } }
以上只適用於一個頁面只有一個的驗證碼的情況,下面改進為可以一個頁面多個驗證碼:
ValidateCode方法改為: public IActionResult ValidateCode(string identify = "") { VerificationCodeServices _vierificationCodeServices = new VerificationCodeServices(); string code = ""; System.IO.MemoryStream ms = _vierificationCodeServices.Create(out code); code = code.ToLower();//驗證碼不分大小寫 Response.Body.Dispose(); var token = Guid.NewGuid().ToString(); ViewBag.token = token; var cacheKey = string.Format(VerificationCodeCacheFormat, token); _memoryCache.Set(cacheKey, code, new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(20))); CookieOptions options = new CookieOptions(); Response.Cookies.Append("validatecode"+ identify, token); return File(ms.ToArray(), @"image/png"); }
當頁面只有一個驗證碼的時候,頁面寫法:
<p> 請輸入驗證碼:<br><input type="text" id="validateCode" class="form-control" /> <img id="imgVerify" src="~/Home/ValidateCode" alt="看不清?點擊更換" onclick="this.src = this.src + '?'" style="vertical-align:middle;" /> </p>
當頁面多個驗證碼:
<p> <input name="verification_code" type="text" maxlength="5" id="code_other" class="form-control tbText"> <img id="imgVerifyOther" src="~/Home/ValidateCode?identify=other" alt="看不清?點擊更換" onclick="this.src = 'ValidateCode?identify=other&' + Math.random()" style="vertical-align:middle;" /> </p>