概述
幾年前,大部分網站、論壇之類的是沒有驗證碼的,因為對于一般用戶來說驗證碼只是增加了用戶的操作,降低了用戶的體驗。但是后來各種灌水機器人、投票機器人、惡意注冊機器人層出不窮,大大增加了網站的負擔同時也給網站數據庫帶來了大量的垃圾數據。為了防止各種機器人程序的破壞,于是程序員想出了只有人眼能夠識別的,程序不容易識別的驗證碼!
驗證碼是一個圖片,將字母、數字甚至漢字作為圖片的內容,這樣一張圖片中的內容用人眼很容易識別,而程序將無法識別。在進行數據庫操作之前(比如登錄驗證、投票、發帖、回復、注冊等等)程序首先驗證客戶端提交的驗證碼是否與圖片中的內容相同,如果相同則進行數據庫操作,不同則提示驗證碼錯誤,不進行數據庫操作。這樣各種機器人程序就被拒之門外了!
驗證碼使用
驗證碼是針對各種機器人程序的,所以驗證碼圖片中的內容是不能存放在Cookie、HTML和URL中的
代碼實現
引用nuget包
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
創建VierificationCodeServices
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;namespace CompanyName.ProjectName.Core
{/// <summary>/// 驗證碼/// </summary>public class VierificationCodeServices{/// <summary>/// 該方法用于生成指定位數的隨機數/// </summary>/// <param name="VcodeNum">參數是隨機數的位數</param>/// <returns>返回一個隨機數字符串</returns>private static string RndNum(int VcodeNum){//驗證碼可以顯示的字符集合string Vchar = "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[] VcArray = Vchar.Split(new Char[] { ',' });//拆分成數組string code = "";//產生的隨機數int temp = -1;//記錄上次隨機數值,盡量避避免生產幾個一樣的隨機數Random rand = new Random();//采用一個簡單的算法以保證生成隨機數的不同for (int i = 1; i < VcodeNum + 1; i++){if (temp != -1){rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));//初始化隨機類}int t = rand.Next(61);//獲取隨機數if (temp != -1 && temp == t){return RndNum(VcodeNum);//如果獲取的隨機數重復,則遞歸調用}temp = t;//把本次產生的隨機數記錄起來code += VcArray[t];//隨機數的位數加一}return code;}/// <summary>/// 該方法是將生成的隨機數寫入圖像文件/// </summary>/// <param name="code">code是一個隨機數</param>/// <param name="numbers">生成位數(默認4位)</param>public static MemoryStream Create(out string code, int numbers = 4){code = RndNum(numbers);Bitmap Img = null;Graphics g = null;MemoryStream ms = null;Random random = new Random();//驗證碼顏色集合Color[] c = { 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);g = Graphics.FromImage(Img);//從Img對象生成新的Graphics對象g.Clear(Color.White);//背景設為白色//在隨機位置畫背景點for (int i = 0; i < 100; i++){int x = random.Next(Img.Width);int y = random.Next(Img.Height);g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);}//驗證碼繪制在g中for (int i = 0; i < code.Length; i++){int cindex = random.Next(7);//隨機顏色索引值int findex = random.Next(5);//隨機字體索引值Font f = new Font(fonts[findex], 15, FontStyle.Bold);//字體Brush b = new SolidBrush(c[cindex]);//顏色int ii = 4;if ((i + 1) % 2 == 0)//控制驗證碼不在同一高度{ii = 2;}g.DrawString(code.Substring(i, 1), f, b, 3 + (i * 12), ii);//繪制一個驗證字符}ms = new MemoryStream();//生成內存流對象Img.Save(ms, ImageFormat.Jpeg);//將此圖像以Png圖像文件的格式保存到流中//回收資源g.Dispose();Img.Dispose();return ms;}}
}
生成ValidateCode
/// <summary>/// 圖形驗證碼/// </summary>/// <returns></returns>public IActionResult ValidateCode(){string code = "";MemoryStream ms = VierificationCodeServices.Create(out code);HttpContext.Session.SetString("LoginValidateCode", code);Response.Body.Dispose();return File(ms.ToArray(), @"image/png");}
總結
以上是生活随笔為你收集整理的ASP.NET Core 跨平台图形验证码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。