1
0

ValidateController.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Hangfire;
  2. using Masuit.MyBlogs.Core.Common.Mails;
  3. using Masuit.Tools.Core.Validator;
  4. namespace Masuit.MyBlogs.Core.Controllers;
  5. public sealed class ValidateController : BaseController
  6. {
  7. /// <summary>
  8. /// 发送验证码
  9. /// </summary>
  10. /// <param name="email"></param>
  11. /// <returns></returns>
  12. [HttpPost, ValidateAntiForgeryToken, ResponseCache(Duration = 115, VaryByQueryKeys = new[] { "email" })]
  13. public ActionResult SendCode(string email)
  14. {
  15. var validator = new IsEmailAttribute();
  16. if (!validator.IsValid(email))
  17. {
  18. return ResultData(null, false, validator.ErrorMessage);
  19. }
  20. if (RedisHelper.Exists("get:" + email))
  21. {
  22. RedisHelper.Expire("get:" + email, 120);
  23. return ResultData(null, false, "发送频率限制,请在2分钟后重新尝试发送邮件!请检查你的邮件,若未收到,请检查你的邮箱地址或邮件垃圾箱!");
  24. }
  25. string code = SnowFlake.GetInstance().GetUniqueShortId(6);
  26. RedisHelper.Set("code:" + email, code, 86400);
  27. BackgroundJob.Enqueue<IMailSender>(sender => sender.Send(Request.Host + "博客验证码", $"{Request.Host}本次验证码是:<span style='color:red'>{code}</span>,有效期为24h,请按时使用!", email, ClientIP.ToString()));
  28. RedisHelper.Set("get:" + email, code, 120);
  29. #if !DEBUG
  30. return ResultData(null, true, "验证码发送成功!");
  31. #else
  32. return ResultData(null, true, "验证码:" + code);
  33. #endif
  34. }
  35. }