예제 #1
0
        /// <summary>
        /// 邮箱校验
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                ErrorMessage = "邮箱不能为空!";
                return(false);
            }

            var email = (string)value;

            if (email.Length < 7)
            {
                ErrorMessage = "您输入的邮箱格式不正确!";
                return(false);
            }

            if (email.Length > 256)
            {
                ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
                return(false);
            }

            if (!string.IsNullOrEmpty(BlockList) && BlockList.Split('!').Any(item => Regex.IsMatch(email, item)))
            {
                ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
                return(false);
            }

            if (!string.IsNullOrEmpty(WhiteList) && WhiteList.Split('!').Any(item => Regex.IsMatch(email, item)))
            {
                return(true);
            }

            if (email.MatchEmail(_valid).isMatch)
            {
                return(true);
            }

            ErrorMessage = "您输入的邮箱格式不正确!";
            return(false);
        }
예제 #2
0
        /// <summary>
        /// 邮箱校验
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value == null)
            {
                ErrorMessage = "邮箱不能为空!";
                return(false);
            }

            var email = (string)value;

            if (email.Length < 7)
            {
                ErrorMessage = "您输入的邮箱格式不正确!";
                return(false);
            }

            if (email.Length > 256)
            {
                ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
                return(false);
            }

            if (!string.IsNullOrEmpty(BlockList) && BlockList.Split(new[] { '!', ';' }, StringSplitOptions.RemoveEmptyEntries).Any(item => Regex.IsMatch(email, item)))
            {
                ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
                return(false);
            }

            if (!string.IsNullOrEmpty(WhiteList) && WhiteList.Split('!').Any(item => Regex.IsMatch(email, item)))
            {
                return(true);
            }

            var isMatch = email.MatchEmail().isMatch;

            if (isMatch && _valid)
            {
                var nslookup = new LookupClient();
                var records  = nslookup.Query(email.Split('@')[1], QueryType.MX).Answers.MxRecords().ToList();
                if (!string.IsNullOrEmpty(BlockList) && records.Any(r => BlockList.Split('!').Any(item => Regex.IsMatch(r.Exchange.Value, item))))
                {
                    ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
                    return(false);
                }

                var task = records.SelectAsync(r => Dns.GetHostAddressesAsync(r.Exchange.Value).ContinueWith(t =>
                {
                    if (t.IsCanceled || t.IsFaulted)
                    {
                        return(new[] { IPAddress.Loopback });
                    }

                    return(t.Result);
                }));
                isMatch = task.Result.SelectMany(a => a).Any(ip => !ip.IsPrivateIP());
            }
            if (isMatch)
            {
                return(true);
            }

            ErrorMessage = "您输入的邮箱格式不正确!";
            return(false);
        }