IsEmailAttribute.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using DnsClient;
  2. using Masuit.Tools.Config;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text.RegularExpressions;
  7. namespace Masuit.Tools.Core.Validator;
  8. /// <summary>
  9. /// 邮箱校验
  10. /// </summary>
  11. public class IsEmailAttribute : ValidationAttribute
  12. {
  13. private readonly bool _valid;
  14. private readonly string _customMessage;
  15. /// <summary>
  16. /// 域白名单
  17. /// </summary>
  18. private string[] WhiteList { get; set; }
  19. /// <summary>
  20. /// 域黑名单
  21. /// </summary>
  22. private string[] BlockList { get; set; }
  23. /// <summary>
  24. /// 是否允许为空
  25. /// </summary>
  26. public bool AllowEmpty { get; set; }
  27. /// <summary>
  28. /// 可在配置文件AppSetting节中添加EmailDomainWhiteList配置邮箱域名白名单,EmailDomainBlockList配置邮箱域名黑名单,英文分号(;)或感叹号(!)或逗号(,)分隔,每个单独的元素支持正则表达式
  29. /// </summary>
  30. /// <param name="validDns">是否检查邮箱DNS记录的有效性</param>
  31. /// <param name="customMessage">自定义错误消息</param>
  32. public IsEmailAttribute(bool validDns = true, string customMessage = null)
  33. {
  34. _valid = validDns;
  35. _customMessage = customMessage;
  36. }
  37. /// <summary>
  38. /// 邮箱校验
  39. /// </summary>
  40. /// <param name="value"></param>
  41. /// <returns></returns>
  42. public override bool IsValid(object value)
  43. {
  44. WhiteList = ConfigHelper.Get<string[]>("EmailDomainWhiteList") ?? [];
  45. BlockList = ConfigHelper.Get<string[]>("EmailDomainBlockList") ?? [];
  46. if (AllowEmpty)
  47. {
  48. switch (value)
  49. {
  50. case null:
  51. case string s when string.IsNullOrEmpty(s):
  52. return true;
  53. }
  54. }
  55. if (value == null)
  56. {
  57. ErrorMessage = _customMessage ?? "邮箱不能为空!";
  58. return false;
  59. }
  60. var email = (string)value;
  61. if (email.Length < 7)
  62. {
  63. ErrorMessage = _customMessage ?? "您输入的邮箱格式不正确!";
  64. return false;
  65. }
  66. if (email.Length > 256)
  67. {
  68. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  69. return false;
  70. }
  71. if (WhiteList.Any(item => Regex.IsMatch(email, item)))
  72. {
  73. return true;
  74. }
  75. if (BlockList.Any(item => Regex.IsMatch(email, item)))
  76. {
  77. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  78. return false;
  79. }
  80. var isMatch = email.MatchEmail().isMatch;
  81. if (isMatch && _valid)
  82. {
  83. var nslookup = new LookupClient();
  84. var records = nslookup.Query(email.Split('@')[1], QueryType.MX).Answers.MxRecords().ToList();
  85. if (records.Exists(r => BlockList.Any(item => Regex.IsMatch(r.Exchange.Value, item))))
  86. {
  87. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  88. return false;
  89. }
  90. var task = records.SelectAsync(r => Dns.GetHostAddressesAsync(r.Exchange.Value).ContinueWith(t =>
  91. {
  92. if (t.IsCanceled || t.IsFaulted)
  93. {
  94. return [IPAddress.Loopback];
  95. }
  96. return t.Result;
  97. }));
  98. isMatch = task.Result.SelectMany(a => a).Any(ip => !ip.IsPrivateIP());
  99. }
  100. if (isMatch)
  101. {
  102. return true;
  103. }
  104. ErrorMessage = _customMessage ?? "您输入的邮箱格式不正确!";
  105. return false;
  106. }
  107. }