登录验证码
//// <summary> /// 生成验证码 /// </summary> /// <param name="length">指定验证码的长度</param> /// <returns></returns> public static 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="chkCode">验证码字符</param> /// <returns></returns> public static byte[] CreateValidateCodePic(string chkCode) { //颜色列表,用于验证码、噪线、噪点 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字体列表,用于验证码 string[] font = { "Times New Roman", "MS Mincho", "Book Antiqua", "Gungsuh", "PMingLiU", "Impact" }; Bitmap bmp = new Bitmap(100, 40); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); Random rnd = new Random(); //画噪线 for (int i = 0; i < 10; i++) { int x1 = rnd.Next(100); int y1 = rnd.Next(40); int x2 = rnd.Next(100); int y2 = rnd.Next(40); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //画验证码字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, 18); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 20 + 8, (float)8); } //画噪点 for (int i = 0; i < 100; i++) { int x = rnd.Next(bmp.Width); int y = rnd.Next(bmp.Height); Color clr = color[rnd.Next(color.Length)]; bmp.SetPixel(x, y, clr); } //将验证码图片写入内存流,并将其以 "image/Png" 格式输出 MemoryStream ms = new MemoryStream(); try { bmp.Save(ms, ImageFormat.Png); return ms.ToArray(); } finally { //显式释放资源 bmp.Dispose(); g.Dispose(); } }
/// <summary> /// 创建验证码字符 /// </summary> /// <param name="length">验证码长度</param> /// <returns></returns> public string CreateValidateCode(int length) { string code = qdatmUtils.CreateValidateCode(length); return code; } /// <summary> /// 创建验证码图片 /// </summary> /// <param name="code">图片中的验证码字符</param> /// <returns></returns> public byte[] CreateValidateCodePic(string code) { var bytes = qdatmUtils.CreateValidateCodePic(code); return bytes; } /// <summary> /// 生成验证码 /// </summary> /// <returns></returns> public ActionResult CheckCode() { string code = CreateValidateCode(4); Session["ValidateCode"] = code; byte[] bytes = CreateValidateCodePic(code); return File(bytes, @"image/png"); }
/// <summary> /// 验证码验证 /// </summary> /// <param name="code"></param> [HttpPost] public ContentResult CheckCode(string code) { bool flag = Session["ValidateCode"].ToString().ToUpper().Equals(code.ToUpper()); if (flag) { return Content("true"); } else { return Content("false"); } }
前台代码:
<div class="input-group" style="display:none" id="vcode"> <input type="text" class="" name="ValidateCode" id="ValidateCode" placeholder="验证码" required maxlength="10"> <img src="/Account/CheckCode?ID=1" id="imgCode" alt="单击可刷新" onclick="ClickRemoveChangeCode()" /> </div>
js:
function ClickRemoveChangeCode() { var code = $("#imgCode").attr("src"); $("#imgCode").attr("src", code + "1"); $("#ValidateCode").val(""); } $(document).ready(function () { $('#loginForm').validate({ rules: { ValidateCode: { required: true, maxlength: 4, remote: { url: "/Account/CheckCode", type: "post", dataType: "json", data: { code: function () { return $("#ValidateCode").val(); } } } } }, messages: { ValidateCode: { required: "验证码不能为空", maxlength: "最大长度4个字符", remote: "验证码错误" } }
创建带字母的验证码:
/// <summary> /// 创建验证码字母 /// </summary> /// <param name="length">验证码的长度</param> /// <returns></returns> public static string CreateValidateCode(int length = 4) { string chkCode = string.Empty; //验证码的字符集,去掉了一些容易混淆的字符 char[] character = { '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); //生成验证码字符串 for (int i = 0; i < length; i++) { chkCode += character[rnd.Next(character.Length)]; } return chkCode; }