應用網址:https://www.cnblogs.com/dayang12525/p/10006957.html
驗證方法:
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>