如何實作C# MVC驗證碼檢核
先產生隨機亂數
並且把亂數存在session
/// <summary>
/// 產生指定長度的隨機數
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private static string RandomCode(int length)
{
string s = "0123456789ZXCVBNMASDFGHJKLQWERTYUIOP";
StringBuilder sb = new StringBuilder();
Random rand = new Random();
int index;
for (int i = 0; i < length; i++)
{
index = rand.Next(0, s.Length);
sb.Append(s[index]);
}
return sb.ToString();
}
HttpContext.Current.Session["CaptchaCode"] = code;
隨機顏色:
/// <summary>
/// 隨機Brushe
/// </summary>
/// <returns></returns>
private static Brush GetRandomBrushe()
{
Brush[] brus = new Brush[] {
Brushes.Black,
Brushes.Red,
Brushes.DarkBlue,
Brushes.Brown,
Brushes.DarkCyan,
Brushes.Purple
};
Random rand = new Random();
int index;
index = rand.Next(0, brus.Length);
return brus[index];
}
範例程式如下:
public static byte[] GetCaptchaCode(int len)
{
byte[] data;
string code = RandomCode(len);
HttpContext.Current.Session["CaptchaCode"] = code;
MemoryStream ms = new MemoryStream();
using (Bitmap map = new Bitmap(100, 40))
using (Graphics g = Graphics.FromImage(map))
{
// 背景色
g.Clear(Color.White);
var brushe = GetRandomBrushe();
g.DrawString(code, new Font("黑體", 18.0F), brushe, new Point(10, 8));
//繪製干擾線(數字代表幾條)
var lineColor = GetRandomBrushe();
while (lineColor == brushe)
{
lineColor = GetRandomBrushe();
}
PaintInterLine(g, lineColor, 10, map.Width, map.Height);
// 繪製干擾點
var pointColor = GetRandomBrushe();
while (pointColor == lineColor || pointColor == brushe)
{
pointColor = GetRandomBrushe();
}
PaintInterPoint(g, pointColor, 50, map.Width, map.Height);
map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
}
data = ms.GetBuffer();
return data;
}
這邊是產生一個Byte之後產生回給前端一個檔案,但這個檔案並不存在伺服器上
不然驗證碼圖片的容量也蠻可觀的,驗證方式在拿Session跟輸入做核對就可以了
/// <summary>
/// 取得及顯示驗證碼
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult GetValidateCaptchaCode()
{
byte[] data = CaptchaHelper.GetCaptchaCode(4);
return File(data, "image/jpeg");
}
實作完成範例: