MVC验证码的编写

8/10/2015来源:ASP.NET技巧人气:2278

MVC验证码的编写

主要是相互学习一下mvc,希望各位大神指导

 1 /// <summary> 2         /// 生成随机数字 3         /// </summary> 4         /// <returns>随机数字</returns> 5         public string GetCode(int len = 4) 6         { 7             string strCode = string.Empty; 8             char code; 9             int num;10             Random rnd = new Random();11             for (int i = 0; i < len; i++)12             {13                 num = rnd.Next();14                 if (num % 2 == 0)15                 {16                     code = (char)('0' + (char)(num % 10));17                 }18                 else19                 {20                     //code = (char)('0' + (char)(num % 10));21                    code = (char)('A' + (char)(num % 26));22                 }23                 strCode += code.ToString();24             }25             return strCode;26         }
生成随机数
 1         /// <summary> 2         /// 生成验证码(保存验证码到图片) 3         /// </summary> 4         /// <returns>二进制</returns> 5         public byte[] PRocessRequestImage(string validateCode) 6         { 7  8             HttpContext context = HttpContext.Current; 9             using (Bitmap bmp = new Bitmap(validateCode.Length * 15 + 10, 28))10             {11                 Graphics g = Graphics.FromImage(bmp);12                 g.Clear(Color.White);13                 Random rnd = new Random();14                 for (int i = 0; i < 2; i++)15                 {16                     int x1 = rnd.Next(bmp.Width);17                     int y1 = rnd.Next(bmp.Height);18                     int x2 = rnd.Next(bmp.Width);19                     int y2 = rnd.Next(bmp.Height);20                     g.DrawLine(new Pen(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))), x1, y1, x2, y2);21                 }22                 g.DrawRectangle(new Pen(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))), new Rectangle(0, 0, bmp.Width - 1, bmp.Height - 1));23                 for (int i = 0; i < 25; i++)24                 {25                     bmp.SetPixel(rnd.Next(bmp.Width), rnd.Next(bmp.Height), Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255)));26                 }27                 for (int i = 0; i < validateCode.Length; i++)28                 {29                     FontFamily[] familys = FontFamily.Families;30                     FontFamily family = familys[rnd.Next(familys.Length)];31                     g.DrawString(validateCode[i].ToString(), new Font(family, 14, FontStyle.Bold), new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))), new PointF(1 + i * 15, 1));32                 }33 34                 //保存图片数据35                 MemoryStream stream = new MemoryStream();36                 bmp.Save(stream, ImageFormat.Jpeg);37 38                 //  输出图片流39                 return stream.ToArray();40             }41         }
生成验证码图片
 1      /// <summary> 2         /// 验证码 3         /// </summary> 4         /// <returns></returns>  5         public ActionResult VerifyCode() 6         { 7             h.Base.Public.SecurityCode seCode = new h.Base.Public.SecurityCode(); 8             var code = seCode.GetCode(); 9             session["code"] = code;10             var bytes = seCode.ProcessRequestImage(code);11             return File(bytes, "image/jpeg");12         }
控制器代码
 1   <div class="pdiv"> 2                 <input type="image" id="valiCode"     src="/Based/VerifyCode" /> 3 </div> 4  5 <script> 6     (function () { 7   //刷新验证码 8         var codeUrl = "/Based/VerifyCode"; 9         $(valiCode).on("click", function () {10             this.src = codeUrl + "?time=" + (new Date()).getTime();11         });12 13     })(jQuery)14 </script>
view视图代码

这段代码我测试过了。。 是可以通过的。