GeneralCaptchaService.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using Microsoft.Extensions.Caching.Memory;
  2. using System.DrawingCore;
  3. using System.DrawingCore.Imaging;
  4. using System.Text;
  5. using YBEE.EQM.Core;
  6. namespace YBEE.EQM.Application
  7. {
  8. /// <summary>
  9. /// 常规验证码
  10. /// </summary>
  11. public class GeneralCaptchaService : IGeneralCaptchaService, ITransient
  12. {
  13. private readonly IMemoryCache _memoryCache;
  14. public GeneralCaptchaService(IMemoryCache memoryCache)
  15. {
  16. _memoryCache = memoryCache;
  17. }
  18. /// <summary>
  19. /// 生成验证码图片
  20. /// </summary>
  21. /// <param name="length">验证码长度</param>
  22. /// <param name="width">图片宽度</param>
  23. /// <param name="height">图片高度</param>
  24. /// <param name="fontSize">字体大小</param>
  25. /// <returns></returns>
  26. public GeneralCaptchaOutput CreateCaptchaImage(int length = 6, int width = 260, int height = 36, int fontSize = 22)
  27. {
  28. var rtnResult = new GeneralCaptchaOutput();
  29. var code = GenerateRandom(length); // 随机字符串集合
  30. rtnResult.ImageBase64 = Convert.ToBase64String(Draw(code, width, height, fontSize));
  31. rtnResult.Token = Guid.NewGuid().ToString();
  32. // 缓存验证码正确位置集合
  33. var cacheOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(120));
  34. _memoryCache.Set(CommonConst.CACHE_KEY_CODE + rtnResult.Token, code, cacheOptions);
  35. return rtnResult;
  36. }
  37. /// <summary>
  38. /// 验证码验证
  39. /// </summary>
  40. /// <param name="input"></param>
  41. /// <returns></returns>
  42. public GeneralCaptchaOutput CheckCode(GeneralCaptchaInput input)
  43. {
  44. var res = new GeneralCaptchaOutput();
  45. var code = _memoryCache.Get(CommonConst.CACHE_KEY_CODE + input.Token);
  46. if (code == null)
  47. {
  48. res.Code = 1;
  49. res.Message = "验证码已失效,请重新获取";
  50. return res;
  51. }
  52. if (string.Compare(input.CaptchaCode, (string)code, true) != 0)
  53. {
  54. res.Code = 2;
  55. res.Message = "验证码错误";
  56. return res;
  57. }
  58. _memoryCache.Remove(CommonConst.CACHE_KEY_CODE + input.Token);
  59. res.Code = 0;
  60. res.Message = "验证成功";
  61. return res;
  62. }
  63. #region 私有方法
  64. /// <summary>
  65. /// 绘制验证码
  66. /// </summary>
  67. /// <param name="code"></param>
  68. /// <param name="codeW"></param>
  69. /// <param name="codeH"></param>
  70. /// <param name="fontSize"></param>
  71. /// <returns></returns>
  72. private static byte[] Draw(string code, int codeW, int codeH, int fontSize)
  73. {
  74. // 颜色列表,用于验证码、噪线、噪点
  75. Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
  76. // 字体列表,用于验证码
  77. string[] fonts = new[] { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
  78. using (var bmp = new Bitmap(codeW, codeH))
  79. using (var g = Graphics.FromImage(bmp))
  80. using (var ms = new MemoryStream())
  81. {
  82. g.Clear(Color.White);
  83. var rnd = new Random();
  84. // 画噪线
  85. for (int i = 0; i < 4; i++)
  86. {
  87. int x1 = rnd.Next(codeW);
  88. int y1 = rnd.Next(codeH);
  89. int x2 = rnd.Next(codeW);
  90. int y2 = rnd.Next(codeH);
  91. var clr = color[rnd.Next(color.Length)];
  92. g.DrawLine(new Pen(clr), x1, y1, x2, y2);
  93. }
  94. // 画验证码字符串
  95. string fnt;
  96. Font ft;
  97. for (int i = 0; i < code.Length; i++)
  98. {
  99. fnt = fonts[rnd.Next(fonts.Length)];
  100. ft = new Font(fnt, fontSize);
  101. var clr = color[rnd.Next(color.Length)];
  102. g.DrawString(code[i].ToString(), ft, new SolidBrush(clr), (float)i * 24 + 3, (float)0);
  103. }
  104. // 将验证码图片写入内存流
  105. bmp.Save(ms, ImageFormat.Png);
  106. return ms.ToArray();
  107. }
  108. }
  109. /// <summary>
  110. /// 生成验证码
  111. /// </summary>
  112. /// <param name="length"></param>
  113. /// <returns></returns>
  114. private static string GenerateRandom(int length)
  115. {
  116. var chars = new StringBuilder();
  117. // 验证码的字符集,去掉了一些容易混淆的字符
  118. char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
  119. Random rnd = new();
  120. // 生成验证码字符串
  121. for (int i = 0; i < length; i++)
  122. {
  123. chars.Append(character[rnd.Next(character.Length)]);
  124. }
  125. return chars.ToString();
  126. }
  127. #endregion
  128. }
  129. }