在mvc 出現之前 生成驗證碼思路
在一個html頁面上,生成一個驗證碼,在把這個頁面嵌入到需要驗證碼的頁面中。
JS生成驗證碼
<script type="text/javascript">jQuery(function ($) {/**生成一個隨機數**/function randomNum(min, max) {return Math.floor(Math.random() * (max - min) + min);}/**生成一個隨機色**/function randomColor(min, max) {var r = randomNum(min, max);var g = randomNum(min, max);var b = randomNum(min, max);return "rgb(" + r + "," + g + "," + b + ")";}var code = drawPic();document.getElementById("changeImg").onclick = function (e) {e.preventDefault();code = drawPic();}/**繪制驗證碼圖片**/function drawPic() {var canvas = document.getElementById("canvas");var width = canvas.width;var height = canvas.height;//獲取該canvas的2D繪圖環境 var ctx = canvas.getContext('2d');ctx.textBaseline = 'bottom';/**繪制背景色**/ctx.fillStyle = randomColor(180, 240);//顏色若太深可能導致看不清ctx.fillRect(0, 0, width, height);/**繪制文字**/var str = 'ABCEFGHJKLMNPQRSTWXY123456789';var code = "";//生成四個驗證碼for (var i = 1; i <= 4; i++) {var txt = str[randomNum(0, str.length)];code = code + txt;ctx.fillStyle = randomColor(50, 160);//隨機生成字體顏色ctx.font = randomNum(15, 40) + 'px SimHei';//隨機生成字體大小var x = 10 + i * 25;var y = randomNum(25, 35);var deg = randomNum(-45, 45);//修改坐標原點和旋轉角度ctx.translate(x, y);ctx.rotate(deg * Math.PI / 180);ctx.fillText(txt, 0, 0);//恢復坐標原點和旋轉角度ctx.rotate(-deg * Math.PI / 180);ctx.translate(-x, -y);}/**繪制干擾線**/for (var i = 0; i < 3; i++) {ctx.strokeStyle = randomColor(40, 180);ctx.beginPath();ctx.moveTo(randomNum(0, width / 2), randomNum(0, height / 2));ctx.lineTo(randomNum(0, width / 2), randomNum(0, height));ctx.stroke();}/**繪制干擾點**/for (var i = 0; i < 50; i++) {ctx.fillStyle = randomColor(255);ctx.beginPath();ctx.arc(randomNum(0, width), randomNum(0, height), 1, 0, 2 * Math.PI);ctx.fill();}$("#code").val(code);return code;}});</script>
MVC中驗證碼
》》》定義一個生成驗證碼的類
public class VerifyCodeHelper{public static Bitmap CreateVerifyCode(out string code){//建立Bitmap對象,繪圖Bitmap bitmap = new Bitmap(200, 60);Graphics graph = Graphics.FromImage(bitmap);graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);Random r = new Random();string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";StringBuilder sb = new StringBuilder();//添加隨機的五個字母for (int x = 0; x < 5; x++){string letter = letters.Substring(r.Next(0, letters.Length - 1), 1);sb.Append(letter);graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));}code = sb.ToString();//混淆背景Pen linePen = new Pen(new SolidBrush(Color.Black), 2);for (int x = 0; x < 6; x++)graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));return bitmap;}}
》》》》Action
/// <summary>/// 驗證碼 FileContentResult/// </summary>/// <returns></returns>[AllowAnonymous]public ActionResult VerifyCode(){string code = "";Bitmap bitmap = VerifyCodeHelper.CreateVerifyCode(out code); //聲明一個內存流 MemoryStream stream = new MemoryStream();//把bitmap存入這個 流中bitmap.Save(stream, ImageFormat.Gif);// Controller中File方法 返回 FileContentResult, // FileContentResult繼承FileResult,FileResult又繼承 ActionResultreturn File(stream.ToArray(), "image/gif");//返回FileContentResult圖片}/// <summary>/// 驗證碼 直接寫入Response/// </summary>[AllowAnonymous]public void Verify(){string code = "";Bitmap bitmap = VerifyCodeHelper.CreateVerifyCode(out code);bitmap.Save(base.Response.OutputStream, ImageFormat.Gif);base.Response.ContentType = "image/gif";}
在html頁面中