MVC验证码
Home控制器
1 using bigtree.Actions; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7 8 namespace MvcVcode.Content 9 { 10 public class HomeController : Controller 11 { 12 // 13 // GET: /Home/ 14 15 public ActionResult Index() 16 { 17 return View(); 18 } 19 20 /// <summary> 21 /// 调用Itcast.CMS.Common类库中的ValidateCode类 来显示验证码 22 /// </summary> 23 /// <param name="str">无用处的参数</param> 24 /// <returns>图片文件</returns> 25 public ActionResult ShowValiDataCode(string str) 26 { 27 ValidateCode vcode = new ValidateCode(); 28 string code = vcode.CreateValidateCode(5); 29 Session["vCode"] = code; 30 return File(vcode.CreateValidateGraphic(code), "image/JPEG"); 31 } 32 33 34 } 35 }
视图
1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>Index</title> 11 <script src="~/Scripts/jquery-1.8.2.js"></script> 12 </head> 13 <body> 14 <div> 15 <img src="Home/ShowValiDataCode/?id=1" class="validataCode" id="ImageCode" /> 16 </div> 17 </body> 18 </html> 19 <script type="text/javascript"> 20 $(function () { 21 $(".validataCode").click(function () { 22 replaceVcode(); 23 }) 24 }) 25 26 function replaceVcode() { 27 $("#ImageCode").attr("src", $("#ImageCode").attr("src") + 1);/**/ 28 } 29 function afterLogin(data) { // 30 31 var serverData = data.split(":"); //对请求返还的出数据进行处理;例如这条数据(ok:登陆成功) 32 if (serverData[0] == "ok") { 33 $("#serverMes").text(serverData[1]); 34 window.location.href = "/Login/Login"; //如果登陆成功(ok)就跳转到主页 35 } else if (serverData[0] == "no") { 36 $("#serverMes").text(serverData[1]); //提示错误 37 replaceVcode(); //刷新验证码 38 } 39 } 40 </script>
验证码代码ValidateCode.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Drawing; 6 using System.Drawing.Drawing2D; 7 using System.IO; 8 using System.Drawing.Imaging; 9 10 namespace bigtree.Actions 11 { 12 public class ValidateCode 13 { 14 //// <summary> 15 /// 生成验证码 16 /// </summary> 17 /// <param name="length">指定验证码的长度</param> 18 /// <returns></returns> 19 public string CreateValidateCode(int length) 20 { 21 int[] randMembers = new int[length]; 22 int[] validateNums = new int[length]; 23 string validateNumberStr = ""; 24 //生成起始序列值 25 int seekSeek = unchecked((int)DateTime.Now.Ticks); 26 Random seekRand = new Random(seekSeek); 27 int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000); 28 int[] seeks = new int[length]; 29 for (int i = 0; i < length; i++) 30 { 31 beginSeek += 10000; 32 seeks[i] = beginSeek; 33 } 34 //生成随机数字 35 for (int i = 0; i < length; i++) 36 { 37 Random rand = new Random(seeks[i]); 38 int pownum = 1 * (int)Math.Pow(10, length); 39 randMembers[i] = rand.Next(pownum, Int32.MaxValue); 40 } 41 //抽取随机数字 42 for (int i = 0; i < length; i++) 43 { 44 string numStr = randMembers[i].ToString(); 45 int numLength = numStr.Length; 46 Random rand = new Random(); 47 int numPosition = rand.Next(0, numLength - 1); 48 validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); 49 } 50 //生成验证码 51 for (int i = 0; i < length; i++) 52 { 53 validateNumberStr += validateNums[i].ToString(); 54 } 55 return validateNumberStr; 56 } 57 58 59 60 61 62 /// <summary> 63 /// 创建验证码的图片 64 /// </summary> 65 /// <param name="containsPage">要输出到的page对象</param> 66 /// <param name="validateNum">验证码</param> 67 public byte[] CreateValidateGraphic(string validateCode) 68 { 69 Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22); 70 Graphics g = Graphics.FromImage(image); 71 try 72 { 73 //生成随机生成器 74 Random random = new Random(); 75 //清空图片背景色 76 g.Clear(Color.White); 77 //画图片的干扰线 78 for (int i = 0; i < 25; i++) 79 { 80 int x1 = random.Next(image.Width); 81 int x2 = random.Next(image.Width); 82 int y1 = random.Next(image.Height); 83 int y2 = random.Next(image.Height); 84 g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); 85 } 86 Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); 87 LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), 88 Color.Blue, Color.DarkRed, 1.2f, true); 89 g.DrawString(validateCode, font, brush, 3, 2); 90 //画图片的前景干扰点 91 for (int i = 0; i < 100; i++) 92 { 93 int x = random.Next(image.Width); 94 int y = random.Next(image.Height); 95 image.SetPixel(x, y, Color.FromArgb(random.Next())); 96 } 97 //画图片的边框线 98 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); 99 //保存图片数据 100 MemoryStream stream = new MemoryStream(); 101 image.Save(stream, ImageFormat.Jpeg); 102 //输出图片流 103 return stream.ToArray(); 104 } 105 finally 106 { 107 g.Dispose(); 108 image.Dispose(); 109 } 110 } 111 } 112 }
效果图片如下