MVC小系列(六)【无刷新的验证码】
做个无刷新的验证码功能:
第一步:首先,在公用项目中建立一个生成图片验证码的类型ValidateCode
1 /// <summary> 2 /// 生成验证码对象 3 /// </summary> 4 public class ValidateCode 5 { 6 public ValidateCode() 7 { 8 } 9 ///<summary> 10 /// 验证码的最大长度 11 ///</summary> 12 public int MaxLength 13 { 14 get { return 10; } 15 } 16 ///<summary> 17 /// 验证码的最小长度 18 ///</summary> 19 public int MinLength 20 { 21 get { return 1; } 22 } 23 ///<summary> 24 /// 生成验证码 25 ///</summary> 26 ///<param name="length">指定验证码的长度</param> 27 ///<returns></returns> 28 public string CreateValidateCode(int length) 29 { 30 int[] randMembers = new int[length]; 31 int[] validateNums = new int[length]; 32 string validateNumberStr = ""; 33 //生成起始序列值 34 int seekSeek = unchecked((int)DateTime.Now.Ticks); 35 Random seekRand = new Random(seekSeek); 36 int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000); 37 int[] seeks = new int[length]; 38 for (int i = 0; i < length; i++) 39 { 40 beginSeek += 10000; 41 seeks[i] = beginSeek; 42 } 43 //生成随机数字 44 for (int i = 0; i < length; i++) 45 { 46 Random rand = new Random(seeks[i]); 47 int pownum = 1 * (int)Math.Pow(10, length); 48 randMembers[i] = rand.Next(pownum, Int32.MaxValue); 49 } 50 //抽取随机数字 51 for (int i = 0; i < length; i++) 52 { 53 string numStr = randMembers[i].ToString(); 54 int numLength = numStr.Length; 55 Random rand = new Random(); 56 int numPosition = rand.Next(0, numLength - 1); 57 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); 58 } 59 //生成验证码 60 for (int i = 0; i < length; i++) 61 { 62 validateNumberStr += validateNums[i].ToString(); 63 } 64 return validateNumberStr; 65 } 66 ///<summary> 67 /// 创建验证码的图片 68 ///</summary> 69 ///<param name="containsPage">要输出到的page对象</param> 70 ///<param name="validateNum">验证码</param> 71 public byte[] CreateValidateGraphic(string validateCode) 72 { 73 Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 14.0), 22); 74 Graphics g = Graphics.FromImage(image); 75 try 76 { 77 //生成随机生成器 78 Random random = new Random(); 79 //清空图片背景色 80 g.Clear(Color.White); 81 //画图片的干扰线 82 for (int i = 0; i < 25; i++) 83 { 84 int x1 = random.Next(image.Width); 85 int x2 = random.Next(image.Width); 86 int y1 = random.Next(image.Height); 87 int y2 = random.Next(image.Height); 88 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 89 } 90 Font font = new Font("Arial", 14, (FontStyle.Bold | FontStyle.Italic)); 91 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 92 Color.Blue, Color.DarkRed, 1.5f, true); 93 g.DrawString(validateCode, font, brush, 3, 2); 94 //画图片的前景干扰点 95 for (int i = 0; i < 100; i++) 96 { 97 int x = random.Next(image.Width); 98 int y = random.Next(image.Height); 99 image.SetPixel(x, y, Color.FromArgb(random.Next())); 100 } 101 //画图片的边框线 102 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 103 //保存图片数据 104 MemoryStream stream = new MemoryStream(); 105 image.Save(stream, ImageFormat.Jpeg); 106 //输出图片流 107 return stream.ToArray(); 108 } 109 finally 110 { 111 g.Dispose(); 112 image.Dispose(); 113 } 114 } 115 }
第二步:
在当前公用的controller中添加一个返回文件类型的action,当然也可以是所有返回类型的基类ActionResult,这个方法用来返回一个图像对象
1 /// <summary> 2 /// 生成验证码图像对象 3 /// </summary> 4 /// <returns></returns> 5 public ActionResult GetValidateCode() 6 { 7 ValidateCode vCode = new ValidateCode(); 8 string code = vCode.CreateValidateCode(4); 9 Session["ValidateCode"] = code; 10 byte[] bytes = vCode.CreateValidateGraphic(code); 11 return File(bytes, @"image/jpeg"); 12 } 13 /// <summary> 14 /// 得到当前验证码 15 /// </summary> 16 /// <returns></returns> 17 public ActionResult GetCurrentValidateCode() 18 { 19 return Content(Session["ValidateCode"].ToString()); 20 }
第三步:在View页面显示
1 <div id="ValidateCodeSpan"> 2 请输入验证码:@Html.TextBox("VCode") 3 <img id="valiCode" style="cursor: pointer;" src="/Home/GetValidateCode" alt="看不清?请点我" /> 4 @Html.Hidden("ValidateCode") 5 6 <script type="text/javascript"> 7 $(function () { 8 //首次加载 9 $("#valiCode").attr("src", "/Home/GetValidateCode?time=" + (new Date()).getTime()); 10 $.get("/Home/GetCurrentValidateCode", function (data) { 11 $("#ValidateCode").val(data); 12 }); 13 //单击验证码事件 14 $("#valiCode").bind("click", function () { 15 this.src = "/Home/GetValidateCode?time=" + (new Date()).getTime(); 16 $.get("/Home/GetCurrentValidateCode", function (data) { 17 $("#ValidateCode").val(data); 18 }); 19 }); 20 }); 21 </script> 22 23 </div>