VarifyCode.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. namespace iNethinkCMS.Helper
  6. {
  7. public class VarifyCode
  8. {
  9. /// <summary>
  10. /// 验证码过期时间,默认10
  11. /// </summary>
  12. public static int Expires { get; set; } = 10;
  13. /// <summary>
  14. /// 返回 新的验证码
  15. /// 注:已经做了过期检测,有效期内只返回同一个验证码
  16. /// </summary>
  17. /// <param name="tel"></param>
  18. /// <returns></returns>
  19. public static int Next(string tel) {
  20. int code = 0;
  21. if (HttpContext.Current.Session[tel] != null)
  22. {
  23. code = (int)HttpContext.Current.Session[tel];
  24. }
  25. else {
  26. code = new Random().Next(100000, 999999);
  27. HttpContext.Current.Session[tel] = code;
  28. HttpContext.Current.Session.Timeout = Expires;
  29. }
  30. return code;
  31. }
  32. public static bool isCodeValidate(string tel, int code)
  33. {
  34. if (code == (int)HttpContext.Current.Session[tel])
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. }
  44. }