IsEmailAttribute.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// <summary>
  17. /// 域白名单
  18. /// </summary>
  19. private string WhiteList { get; }
  20. /// <summary>
  21. /// 域黑名单
  22. /// </summary>
  23. private string BlockList { get; }
  24. /// <summary>
  25. /// 可在配置文件AppSetting节中添加EmailDomainWhiteList配置邮箱域名白名单,EmailDomainBlockList配置邮箱域名黑名单,英文分号(;)或感叹号(!)或逗号(,)分隔,每个单独的元素支持正则表达式
  26. /// </summary>
  27. /// <param name="valid">是否检查邮箱的有效性</param>
  28. public IsEmailAttribute(bool valid = true)
  29. {
  30. WhiteList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainWhiteList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
  31. BlockList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainBlockList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
  32. _valid = valid;
  33. }
  34. /// <summary>
  35. /// 邮箱校验
  36. /// </summary>
  37. /// <param name="value"></param>
  38. /// <returns></returns>
  39. public override bool IsValid(object value)
  40. {
  41. if (value == null)
  42. {
  43. ErrorMessage = "邮箱不能为空!";
  44. return false;
  45. }
  46. var email = (string)value;
  47. if (email.Length < 7)
  48. {
  49. ErrorMessage = "您输入的邮箱格式不正确!";
  50. return false;
  51. }
  52. if (email.Length > 256)
  53. {
  54. ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  55. return false;
  56. }
  57. if (!string.IsNullOrEmpty(BlockList) && BlockList.Split(new[] { '!', ';' }, StringSplitOptions.RemoveEmptyEntries).Any(item => Regex.IsMatch(email, item)))
  58. {
  59. ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  60. return false;
  61. }
  62. if (!string.IsNullOrEmpty(WhiteList) && WhiteList.Split('!').Any(item => Regex.IsMatch(email, item)))
  63. {
  64. return true;
  65. }
  66. var isMatch = email.MatchEmail().isMatch;
  67. if (isMatch && _valid)
  68. {
  69. var nslookup = new LookupClient();
  70. var records = nslookup.Query(email.Split('@')[1], QueryType.MX).Answers.MxRecords().ToList();
  71. if (!string.IsNullOrEmpty(BlockList) && records.Any(r => BlockList.Split('!').Any(item => Regex.IsMatch(r.Exchange.Value, item))))
  72. {
  73. ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
  74. return false;
  75. }
  76. var task = records.SelectAsync(r => Dns.GetHostAddressesAsync(r.Exchange.Value).ContinueWith(t =>
  77. {
  78. if (t.IsCanceled || t.IsFaulted)
  79. {
  80. return new[] { IPAddress.Loopback };
  81. }
  82. return t.Result;
  83. }));
  84. isMatch = task.Result.SelectMany(a => a).Any(ip => !ip.IsPrivateIP());
  85. }
  86. if (isMatch)
  87. {
  88. return true;
  89. }
  90. ErrorMessage = "您输入的邮箱格式不正确!";
  91. return false;
  92. }
  93. }
  94. }