IsEmailAttribute.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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. public IsEmailAttribute(bool valid = true)
  34. {
  35. WhiteList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainWhiteList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
  36. BlockList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainBlockList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
  37. _valid = valid;
  38. _customMessage = ErrorMessage;
  39. }
  40. /// <summary>
  41. /// 邮箱校验
  42. /// </summary>
  43. /// <param name="value"></param>
  44. /// <returns></returns>
  45. public override bool IsValid(object value)
  46. {
  47. if (value == null && !AllowEmpty)
  48. {
  49. ErrorMessage = _customMessage ?? "邮箱不能为空!";
  50. return false;
  51. }
  52. var email = (string)value;
  53. if (email.Length < 7)
  54. {
  55. ErrorMessage = _customMessage ?? "您输入的邮箱格式不正确!";
  56. return false;
  57. }
  58. if (email.Length > 256)
  59. {
  60. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  61. return false;
  62. }
  63. if (!string.IsNullOrEmpty(BlockList) && BlockList.Split(new[] { '!', ';' }, StringSplitOptions.RemoveEmptyEntries).Any(item => Regex.IsMatch(email, item)))
  64. {
  65. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  66. return false;
  67. }
  68. if (!string.IsNullOrEmpty(WhiteList) && WhiteList.Split('!').Any(item => Regex.IsMatch(email, item)))
  69. {
  70. return true;
  71. }
  72. var isMatch = email.MatchEmail().isMatch;
  73. if (isMatch && _valid)
  74. {
  75. var nslookup = new LookupClient();
  76. var records = nslookup.Query(email.Split('@')[1], QueryType.MX).Answers.MxRecords().ToList();
  77. if (!string.IsNullOrEmpty(BlockList) && records.Any(r => BlockList.Split('!').Any(item => Regex.IsMatch(r.Exchange.Value, item))))
  78. {
  79. ErrorMessage = _customMessage ?? "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  80. return false;
  81. }
  82. var task = records.SelectAsync(r => Dns.GetHostAddressesAsync(r.Exchange.Value).ContinueWith(t =>
  83. {
  84. if (t.IsCanceled || t.IsFaulted)
  85. {
  86. return new[] { IPAddress.Loopback };
  87. }
  88. return t.Result;
  89. }));
  90. isMatch = task.Result.SelectMany(a => a).Any(ip => !ip.IsPrivateIP());
  91. }
  92. if (isMatch)
  93. {
  94. return true;
  95. }
  96. ErrorMessage = _customMessage ?? "您输入的邮箱格式不正确!";
  97. return false;
  98. }
  99. }
  100. }