IsEmailAttribute.cs 4.4 KB

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