平安校园
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

143 行
4.0 KiB

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