吴劲韬

null

null

.NET MVC 验证码

.NET MVC 验证码

验证码示例图片

效果差不多就这样,接下来直接上代码。

验证码生成器

  1. public class ValidateCode
  2. {
  3. public static ValidateCode Default = new ValidateCode();
  4. public int ImageWidth { get; set; }
  5. public int ImageHeight { get; set; }
  6. public string Letters { get; set; }
  7. public int CodeLength { get; set; }
  8. private Random r = new Random();
  9. public ValidateCode()
  10. {
  11. ImageWidth = 200;
  12. ImageHeight = 60;
  13. CodeLength = 6;
  14. Letters = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ1234567890";
  15. }
  16. public string GetValidationCode()
  17. {
  18. //合法随机显示字符列表
  19. System.Text.StringBuilder s = new System.Text.StringBuilder();
  20. //将随机生成的字符串绘制到图片上
  21. for (int i = 0; i < CodeLength; i++)
  22. {
  23. s.Append(Letters.Substring(r.Next(0, Letters.Length - 1), 1));
  24. }
  25. return s.ToString();
  26. }
  27. public byte[] GetValidationImage(string codeString)
  28. {
  29. //设置输出流图片格式
  30. var b = new System.Drawing.Bitmap(ImageWidth, ImageHeight);
  31. var g = System.Drawing.Graphics.FromImage(b);
  32. int ColorR = r.Next(0, 255);
  33. int ColorG = r.Next(0, 255);
  34. int ColorB = r.Next(0, 255);
  35. g.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(ColorR, ColorG, ColorB)), 0, 0, 200, 60);
  36. var font = new System.Drawing.Font(System.Drawing.FontFamily.GenericSerif, 48, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
  37. //合法随机显示字符列表
  38. //将随机生成的字符串绘制到图片上
  39. for (int i = 0; i < codeString.Length; i++)
  40. {
  41. int sR = r.Next(0, 255);
  42. int sG = r.Next(0, 255);
  43. int sB = r.Next(0, 255);
  44. while (Math.Abs(sR - ColorR) < 35) sR = r.Next(0, 255);
  45. while (Math.Abs(sG - ColorG) < 35) sG = r.Next(0, 255);
  46. while (Math.Abs(sB - ColorB) < 35) sB = r.Next(0, 255);
  47. g.DrawString(codeString[i].ToString(), font, new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(sR, sG, sB)), i * (200 / codeString.Length - 2), r.Next(0, 15));
  48. }
  49. //生成干扰线条
  50. var pen = new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255))), 2);
  51. for (int i = 0; i < 10; i++)
  52. {
  53. g.DrawLine(pen, new System.Drawing.Point(r.Next(0, 199), r.Next(0, 59)), new System.Drawing.Point(r.Next(0, 199), r.Next(0, 59)));
  54. }
  55. var stream = new System.IO.MemoryStream();
  56. b.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
  57. g.Dispose();
  58. b.Dispose();
  59. //输出图片流
  60. return stream.ToArray();
  61. }
  62. }

使用方法

  1. public ActionResult validateCode() //创建一个action用于返回验证码图片
  2. {
  3. string validateCode = ValidateCode.Default.GetValidationCode(); //获取验证码
  4. Session["LoginValidateCode"] = validateCode; //将验证码保存到Session中
  5. return File(ValidateCode.Default.GetValidationImage(validateCode), @"image/jpeg"); //返回验证码图片
  6. }
  7. //直接将这个action 的地址赋值给img标签就可以了,例如 <img src="http://localhost:5695/home/validateCode" alt="">
  8. public ActionResult Login(string validateCode)
  9. {
  10. if (String.IsNullOrEmpty(Session["LoginValidateCode"].ToString()))
  11. {
  12. return Content("你登录页面停留的时间过久页已经超时");
  13. }
  14. else if (!Session["LoginValidateCode"].ToString().Equals(validateCode, StringComparison.CurrentCultureIgnoreCase))
  15. {
  16. return Content("验证码错误");
  17. }
  18. return RedirectToAction("Index"); //成功
  19. }
posted @ 2016-05-07 00:35  吴劲韬  阅读(1611)  评论(1编辑  收藏