平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CaptchaUtil.cs 4.0 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 
  2. //
  3. namespace SafeCampus.Core.Utils;
  4. /// <summary>
  5. /// 生成验证码功能
  6. /// </summary>
  7. public static class CaptchaUtil
  8. {
  9. /// <summary>
  10. /// 获取验证码
  11. /// </summary>
  12. /// <param name="length">验证码数</param>
  13. /// <param name="width">长度</param>
  14. /// <param name="height">高度</param>
  15. /// <param name="fontSize">字体大小</param>
  16. /// <param name="type">类型 0:数字 1:字符 2:计算</param>
  17. /// <returns></returns>
  18. public static CaptchaInfo CreateCaptcha(CaptchaType type = CaptchaType.CHAR, int length = 4, int width = 170,
  19. int height = 50, int fontSize = 20)
  20. {
  21. //初始化验证码
  22. string charCode;
  23. var resultCode = "";
  24. switch (type.ToString())
  25. {
  26. case "NUM":
  27. charCode = CreateNumCode(length);
  28. break;
  29. case "ARITH":
  30. charCode = CreateArithCode(out resultCode);
  31. break;
  32. default:
  33. charCode = CreateCharCode(length);
  34. break;
  35. }
  36. var imageGenerator = new DefaultCaptchaImageGenerator();
  37. var imageGeneratorOption = new CaptchaImageGeneratorOption
  38. {
  39. // 必须设置
  40. ForegroundColors = DefaultColors.Instance.Colors,
  41. Width = width,
  42. Height = height,
  43. FontSize = fontSize,
  44. TextBold = true,
  45. BubbleCount = 1,
  46. FontFamily = DefaultFontFamilys.Instance.Actionj
  47. };
  48. var bytes = imageGenerator.Generate(charCode, imageGeneratorOption);
  49. var captchaInfo = new CaptchaInfo
  50. {
  51. Code = type.ToString() == "ARITH" ? resultCode : charCode,
  52. Image = bytes
  53. };
  54. return captchaInfo;
  55. }
  56. /// <summary>
  57. /// 获取数字验证码
  58. /// </summary>
  59. /// <param name="n">验证码数</param>
  60. /// <returns></returns>
  61. public static string CreateNumCode(int n)
  62. {
  63. char[] numChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  64. var charCode = string.Empty;
  65. var random = new Random();
  66. for (var i = 0; i < n; i++)
  67. {
  68. charCode += numChar[random.Next(numChar.Length)];
  69. }
  70. return charCode;
  71. }
  72. /// <summary>
  73. /// 获取字符验证码
  74. /// </summary>
  75. /// <param name="n">验证码数</param>
  76. /// <returns></returns>
  77. public static string CreateCharCode(int n)
  78. {
  79. char[] strChar =
  80. {
  81. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  82. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  83. '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  84. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  85. };
  86. var charCode = string.Empty;
  87. var random = new Random();
  88. for (var i = 0; i < n; i++)
  89. {
  90. charCode += strChar[random.Next(strChar.Length)];
  91. }
  92. return charCode;
  93. }
  94. /// <summary>
  95. /// 获取运算符验证码
  96. /// </summary>
  97. /// <returns></returns>
  98. public static string CreateArithCode(out string resultCode)
  99. {
  100. string checkCode;
  101. var random = new Random();
  102. var intFirst = random.Next(1, 10);//生成第一个数字
  103. var intSec = random.Next(1, 10);//生成第二个数字
  104. int intTemp;
  105. switch (random.Next(1, 3).ToString())
  106. {
  107. case "2":
  108. if (intFirst < intSec)
  109. {
  110. intTemp = intFirst;
  111. intFirst = intSec;
  112. intSec = intTemp;
  113. }
  114. checkCode = intFirst + "-" + intSec + "=";
  115. resultCode = (intFirst - intSec).ToString();
  116. break;
  117. default:
  118. checkCode = intFirst + "+" + intSec + "=";
  119. resultCode = (intFirst + intSec).ToString();
  120. break;
  121. }
  122. return checkCode;
  123. }
  124. }