MVC验证码
来源:互联网 发布:婚前性行为 佛教 知乎 编辑:程序博客网 时间:2024/05/22 10:59
Home控制器
using bigtree.Actions;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcVcode.Content{ public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } /// <summary> /// 调用Itcast.CMS.Common类库中的ValidateCode类 来显示验证码 /// </summary> /// <param name="str">无用处的参数</param> /// <returns>图片文件</returns> public ActionResult ShowValiDataCode(string str) { ValidateCode vcode = new ValidateCode(); string code = vcode.CreateValidateCode(5); Session["vCode"] = code; return File(vcode.CreateValidateGraphic(code), "image/JPEG"); } }}
视图
@{ Layout = null;}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.8.2.js"></script></head><body> <div> <img src="Home/ShowValiDataCode/?id=1" class="validataCode" id="ImageCode" /> </div></body></html><script type="text/javascript"> $(function () { $(".validataCode").click(function () { replaceVcode(); }) }) function replaceVcode() { $("#ImageCode").attr("src", $("#ImageCode").attr("src") + 1);/**/ } function afterLogin(data) { // var serverData = data.split(":"); //对请求返还的出数据进行处理;例如这条数据(ok:登陆成功) if (serverData[0] == "ok") { $("#serverMes").text(serverData[1]); window.location.href = "/Login/Login"; //如果登陆成功(ok)就跳转到主页 } else if (serverData[0] == "no") { $("#serverMes").text(serverData[1]); //提示错误 replaceVcode(); //刷新验证码 } }</script>
验证码代码ValidateCode.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Drawing;using System.Drawing.Drawing2D;using System.IO;using System.Drawing.Imaging;namespace bigtree.Actions{ public class ValidateCode { //// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长度</param> /// <returns></returns> public string CreateValidateCode(int length) { int[] randMembers = new int[length]; int[] validateNums = new int[length]; string validateNumberStr = ""; //生成起始序列值 int seekSeek = unchecked((int)DateTime.Now.Ticks); Random seekRand = new Random(seekSeek); int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000); int[] seeks = new int[length]; for (int i = 0; i < length; i++) { beginSeek += 10000; seeks[i] = beginSeek; } //生成随机数字 for (int i = 0; i < length; i++) { Random rand = new Random(seeks[i]); int pownum = 1 * (int)Math.Pow(10, length); randMembers[i] = rand.Next(pownum, Int32.MaxValue); } //抽取随机数字 for (int i = 0; i < length; i++) { string numStr = randMembers[i].ToString(); int numLength = numStr.Length; Random rand = new Random(); int numPosition = rand.Next(0, numLength - 1); validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1)); } //生成验证码 for (int i = 0; i < length; i++) { validateNumberStr += validateNums[i].ToString(); } return validateNumberStr; } /// <summary> /// 创建验证码的图片 /// </summary> /// <param name="containsPage">要输出到的page对象</param> /// <param name="validateNum">验证码</param> public byte[] CreateValidateGraphic(string validateCode) { Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 22); Graphics g = Graphics.FromImage(image); try { //生成随机生成器 Random random = new Random(); //清空图片背景色 g.Clear(Color.White); //画图片的干扰线 for (int i = 0; i < 25; i++) { int x1 = random.Next(image.Width); int x2 = random.Next(image.Width); int y1 = random.Next(image.Height); int y2 = random.Next(image.Height); g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2); } Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic)); LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true); g.DrawString(validateCode, font, brush, 3, 2); //画图片的前景干扰点 for (int i = 0; i < 100; i++) { int x = random.Next(image.Width); int y = random.Next(image.Height); image.SetPixel(x, y, Color.FromArgb(random.Next())); } //画图片的边框线 g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1); //保存图片数据 MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); //输出图片流 return stream.ToArray(); } finally { g.Dispose(); image.Dispose(); } } }}
0 0
- MVC 验证码 解决方法
- MVC验证码
- mvc生成验证码
- Spring MVC 验证码
- .net MVC 验证码
- MVC实现验证码
- MVC生成验证码
- MVC验证码
- MVC 验证码
- MVC 登陆验证码
- MVC后端验证码
- MVC 登录验证码
- .NET MVC 使用验证码验证登录
- ASP. net MVC 验证码
- Spring mvc登陆验证码
- asp.net Mvc验证码
- Spring MVC 图片验证码
- Spring MVC生成验证码
- Search for a Range
- iOS 合理利用Clang警告来提高代码质量
- 剑指offer之最小的k个数
- Map容器的使用
- Maximum Subarray
- MVC验证码
- awk
- [041] 微信公众帐号开发教程第17篇-应用实例之智能翻译
- ACM选修(递归)
- Android NDK (学习笔记七) —— JNI的交互处理之Eclipse开发工具配置
- iOS 2d自定义绘图浅析
- 手把手教你玩转SOCKET模型:完成端口(Completion Port)详解
- 浅谈酒店人力资源管理的要点
- [042] 微信公众帐号开发教程第18篇-应用实例之音乐搜索