|
|
@@ -1,6 +1,7 @@
|
|
|
using Masuit.Tools.Core.Config;
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
using System.Linq;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
|
|
|
namespace Masuit.Tools.Core.Validator
|
|
|
{
|
|
|
@@ -14,15 +15,21 @@ namespace Masuit.Tools.Core.Validator
|
|
|
/// <summary>
|
|
|
/// 域白名单
|
|
|
/// </summary>
|
|
|
- private string DomainWhiteList { get; }
|
|
|
+ private string WhiteList { get; }
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 可在配置文件AppSetting节中添加EmailDomainWhiteList配置邮箱域名白名单,逗号分隔
|
|
|
+ /// 域黑名单
|
|
|
+ /// </summary>
|
|
|
+ private string BlockList { get; }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 可在配置文件AppSetting节中添加EmailDomainWhiteList配置邮箱域名白名单,EmailDomainBlockList配置邮箱域名黑名单,逗号分隔,每个单独的元素支持正则表达式
|
|
|
/// </summary>
|
|
|
/// <param name="valid">是否检查邮箱的有效性</param>
|
|
|
public IsEmailAttribute(bool valid = true)
|
|
|
{
|
|
|
- DomainWhiteList = ConfigHelper.GetConfigOrDefault("EmailDomainWhiteList", string.Empty);
|
|
|
+ WhiteList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainWhiteList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
|
|
|
+ BlockList = Regex.Replace(ConfigHelper.GetConfigOrDefault("EmailDomainBlockList"), @"(\w)\.([a-z]+),?", @"$1\.$2!").Trim('!');
|
|
|
_valid = valid;
|
|
|
}
|
|
|
|
|
|
@@ -39,7 +46,7 @@ namespace Masuit.Tools.Core.Validator
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- var email = value as string;
|
|
|
+ var email = (string)value;
|
|
|
if (email.Length < 7)
|
|
|
{
|
|
|
ErrorMessage = "您输入的邮箱格式不正确!";
|
|
|
@@ -48,11 +55,17 @@ namespace Masuit.Tools.Core.Validator
|
|
|
|
|
|
if (email.Length > 256)
|
|
|
{
|
|
|
- ErrorMessage = "邮箱长度最大允许255个字符!";
|
|
|
+ ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!string.IsNullOrEmpty(BlockList) && BlockList.Split('!').Any(item => Regex.IsMatch(email, item)))
|
|
|
+ {
|
|
|
+ ErrorMessage = "您输入的邮箱无效,请使用真实有效的邮箱地址!";
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- if (DomainWhiteList.Split(',').Any(item => email.EndsWith("@" + item)))
|
|
|
+ if (!string.IsNullOrEmpty(WhiteList) && WhiteList.Split('!').Any(item => Regex.IsMatch(email, item)))
|
|
|
{
|
|
|
return true;
|
|
|
}
|