浏览代码

新增一些实体模型校验器:
1.密码强度校验
2.最值校验
3.手机号校验
4.邮箱格式校验
5.IP地址校验

懒得勤快 7 年之前
父节点
当前提交
24dcbe6112

+ 1 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -2,7 +2,7 @@
 
   <PropertyGroup>
     <TargetFramework>netcoreapp2.0</TargetFramework>
-    <Version>2.0.1</Version>
+    <Version>2.1.0</Version>
     <Authors>懒得勤快</Authors>
     <Company>masuit.com</Company>
     <Description>包含一些常用的操作类,大都是静态类,加密解密,反射操作,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,html处理,验证码、NoSql等常用封装。

+ 32 - 0
Masuit.Tools.Core/Validator/ComplexPassword.cs

@@ -0,0 +1,32 @@
+using System.ComponentModel.DataAnnotations;
+using System.Text.RegularExpressions;
+
+namespace Masuit.Tools.Core.Validator
+{
+    /// <summary>
+    /// 强密码验证
+    /// </summary>
+    public class ComplexPassword : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            string pwd = value as string;
+            if (pwd.Length <= 6)
+            {
+                ErrorMessage = "密码过短,至少需要6个字符!";
+                return false;
+            }
+            var regex = new Regex(@"(?=.*[0-9])                     #必须包含数字
+                                            (?=.*[a-zA-Z])                  #必须包含小写或大写字母
+                                            (?=([\x21-\x7e]+)[^a-zA-Z0-9])  #必须包含特殊符号
+                                            .{6,30}                         #至少6个字符,最多30个字符
+                                            ", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
+            if (regex.Match(pwd).Success)
+            {
+                return true;
+            }
+            ErrorMessage = "密码强度值不够,密码必须包含数字,必须包含小写或大写字母,必须包含至少一个特殊符号,至少6个字符,最多30个字符!";
+            return false;
+        }
+    }
+}

+ 35 - 0
Masuit.Tools.Core/Validator/IsEmailAttribute.cs

@@ -0,0 +1,35 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator
+{
+    public class IsEmailAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value == null)
+            {
+                ErrorMessage = "邮箱不能为空!";
+                return false;
+            }
+
+            var email = value as string;
+            if (email.Length < 6)
+            {
+                ErrorMessage = "您输入的邮箱格式不正确!";
+                return false;
+            }
+
+            if (email.Length > 256)
+            {
+                ErrorMessage = "邮箱长度最大允许255个字符!";
+                return false;
+            }
+            if (email.MatchEmail())
+            {
+                return true;
+            }
+            ErrorMessage = "您输入的邮箱格式不正确!";
+            return false;
+        }
+    }
+}

+ 26 - 0
Masuit.Tools.Core/Validator/IsIPAddressAttribute.cs

@@ -0,0 +1,26 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator
+{
+    /// <summary>
+    /// 验证IPv4地址是否合法
+    /// </summary>
+    public class IsIPAddressAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                ErrorMessage = "IP地址不能为空!";
+                return false;
+            }
+            string email = value as string;
+            if (email.MatchInetAddress())
+            {
+                return true;
+            }
+            ErrorMessage = "IP地址格式不正确,请输入有效的IPv4地址";
+            return false;
+        }
+    }
+}

+ 26 - 0
Masuit.Tools.Core/Validator/IsPhoneAttribute.cs

@@ -0,0 +1,26 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator
+{
+    /// <summary>
+    /// 验证手机号码是否合法
+    /// </summary>
+    public class IsPhoneAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                ErrorMessage = "手机号码不能为空";
+                return false;
+            }
+            string phone = value as string;
+            if (phone.MatchPhoneNumber())
+            {
+                return true;
+            }
+            ErrorMessage = "手机号码格式不正确,请输入有效的大陆11位手机号码!";
+            return false;
+        }
+    }
+}

+ 31 - 0
Masuit.Tools.Core/Validator/MaxValueAttribute.cs

@@ -0,0 +1,31 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator
+{
+    public class MaxValueAttribute : ValidationAttribute
+    {
+        private double MaxValue { get; }
+        public MaxValueAttribute(double value)
+        {
+            MaxValue = value;
+        }
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                return true;
+            }
+            var input = Convert.ToDouble(value);
+            return input <= MaxValue;
+        }
+
+        /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>
+        /// <param name="name">The name to include in the formatted message.</param>
+        /// <returns>An instance of the formatted error message.</returns>
+        public override string FormatErrorMessage(string name)
+        {
+            return base.FormatErrorMessage(name);
+        }
+    }
+}

+ 31 - 0
Masuit.Tools.Core/Validator/MinValueAttribute.cs

@@ -0,0 +1,31 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Core.Validator
+{
+    public class MinValueAttribute : ValidationAttribute
+    {
+        private double MinValue { get; set; }
+        public MinValueAttribute(double value)
+        {
+            MinValue = value;
+        }
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                return true;
+            }
+            var input = Convert.ToDouble(value);
+            return input > MinValue;
+        }
+
+        /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>
+        /// <param name="name">The name to include in the formatted message.</param>
+        /// <returns>An instance of the formatted error message.</returns>
+        public override string FormatErrorMessage(string name)
+        {
+            return base.FormatErrorMessage(name);
+        }
+    }
+}

+ 4 - 4
Masuit.Tools.NoSQL.MongoDBClient/Masuit.Tools.NoSQL.MongoDBClient.csproj

@@ -34,14 +34,14 @@
     <Reference Include="AngleSharp, Version=0.9.9.0, Culture=neutral, PublicKeyToken=e83494dcdc6d31ea, processorArchitecture=MSIL">
       <HintPath>..\packages\AngleSharp.0.9.10\lib\net45\AngleSharp.dll</HintPath>
     </Reference>
-    <Reference Include="DnsClient, Version=1.1.1.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
-      <HintPath>..\packages\DnsClient.1.1.1\lib\net45\DnsClient.dll</HintPath>
+    <Reference Include="DnsClient, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
+      <HintPath>..\packages\DnsClient.1.2.0\lib\net45\DnsClient.dll</HintPath>
     </Reference>
     <Reference Include="HtmlSanitizer, Version=3.0.0.0, Culture=neutral, PublicKeyToken=61c49a1a9e79cc28, processorArchitecture=MSIL">
       <HintPath>..\packages\HtmlSanitizer.4.0.187\lib\net45\HtmlSanitizer.dll</HintPath>
     </Reference>
-    <Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
-      <HintPath>..\packages\SharpZipLib.0.86.0\lib\20\ICSharpCode.SharpZipLib.dll</HintPath>
+    <Reference Include="ICSharpCode.SharpZipLib, Version=1.0.0.999, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
+      <HintPath>..\packages\SharpZipLib.1.0.0\lib\net45\ICSharpCode.SharpZipLib.dll</HintPath>
     </Reference>
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="MongoDB.Bson, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">

+ 1 - 1
Masuit.Tools.NoSQL.MongoDBClient/app.config

@@ -16,7 +16,7 @@
       </dependentAssembly>
       <dependentAssembly>
         <assemblyIdentity name="DnsClient" publicKeyToken="4574bb5573c51424" culture="neutral" />
-        <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
+        <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
       </dependentAssembly>
     </assemblyBinding>
   </runtime>

+ 2 - 2
Masuit.Tools.NoSQL.MongoDBClient/packages.config

@@ -1,13 +1,13 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
   <package id="AngleSharp" version="0.9.10" targetFramework="net45" />
-  <package id="DnsClient" version="1.1.1" targetFramework="net45" />
+  <package id="DnsClient" version="1.2.0" targetFramework="net45" />
   <package id="HtmlSanitizer" version="4.0.187" targetFramework="net45" />
   <package id="MongoDB.Bson" version="2.7.0" targetFramework="net45" />
   <package id="MongoDB.Driver" version="2.7.0" targetFramework="net45" />
   <package id="MongoDB.Driver.Core" version="2.7.0" targetFramework="net45" />
   <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
-  <package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
+  <package id="SharpZipLib" version="1.0.0" targetFramework="net45" />
   <package id="StackExchange.Redis" version="1.2.6" targetFramework="net45" />
   <package id="System.Buffers" version="4.4.0" targetFramework="net45" />
   <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net45" />

+ 6 - 0
Masuit.Tools/Masuit.Tools.csproj

@@ -125,6 +125,12 @@
     <Compile Include="Systems\EnumExt.cs" />
     <Compile Include="Systems\Lock.cs" />
     <Compile Include="Systems\RedisLock.cs" />
+    <Compile Include="Validator\ComplexPassword.cs" />
+    <Compile Include="Validator\IsEmailAttribute.cs" />
+    <Compile Include="Validator\IsIPAddressAttribute.cs" />
+    <Compile Include="Validator\IsPhoneAttribute.cs" />
+    <Compile Include="Validator\MaxValueAttribute.cs" />
+    <Compile Include="Validator\MinValueAttribute.cs" />
     <Compile Include="Win32\Windows.cs" />
     <Compile Include="Win32\WindowsCommand.cs" />
   </ItemGroup>

+ 2 - 2
Masuit.Tools/Properties/AssemblyInfo.cs

@@ -36,7 +36,7 @@ using System.Runtime.InteropServices;
 // 方法是按如下所示使用“*”: :
 // [assembly: AssemblyVersion("1.0.*")]
 
-[assembly: AssemblyVersion("2.0.0.0")]
-[assembly: AssemblyFileVersion("2.0.0.0")]
+[assembly: AssemblyVersion("2.1.0.0")]
+[assembly: AssemblyFileVersion("2.1.0.0")]
 [assembly: NeutralResourcesLanguage("zh-CN")]
 

+ 32 - 0
Masuit.Tools/Validator/ComplexPassword.cs

@@ -0,0 +1,32 @@
+using System.ComponentModel.DataAnnotations;
+using System.Text.RegularExpressions;
+
+namespace Masuit.Tools.Validator
+{
+    /// <summary>
+    /// 强密码验证
+    /// </summary>
+    public class ComplexPassword : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            string pwd = value as string;
+            if (pwd.Length <= 6)
+            {
+                ErrorMessage = "密码过短,至少需要6个字符!";
+                return false;
+            }
+            var regex = new Regex(@"(?=.*[0-9])                     #必须包含数字
+                                            (?=.*[a-zA-Z])                  #必须包含小写或大写字母
+                                            (?=([\x21-\x7e]+)[^a-zA-Z0-9])  #必须包含特殊符号
+                                            .{6,30}                         #至少6个字符,最多30个字符
+                                            ", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
+            if (regex.Match(pwd).Success)
+            {
+                return true;
+            }
+            ErrorMessage = "密码强度值不够,密码必须包含数字,必须包含小写或大写字母,必须包含至少一个特殊符号,至少6个字符,最多30个字符!";
+            return false;
+        }
+    }
+}

+ 35 - 0
Masuit.Tools/Validator/IsEmailAttribute.cs

@@ -0,0 +1,35 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Validator
+{
+    public class IsEmailAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value == null)
+            {
+                ErrorMessage = "邮箱不能为空!";
+                return false;
+            }
+
+            var email = value as string;
+            if (email.Length < 6)
+            {
+                ErrorMessage = "您输入的邮箱格式不正确!";
+                return false;
+            }
+
+            if (email.Length > 256)
+            {
+                ErrorMessage = "邮箱长度最大允许255个字符!";
+                return false;
+            }
+            if (email.MatchEmail())
+            {
+                return true;
+            }
+            ErrorMessage = "您输入的邮箱格式不正确!";
+            return false;
+        }
+    }
+}

+ 26 - 0
Masuit.Tools/Validator/IsIPAddressAttribute.cs

@@ -0,0 +1,26 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Validator
+{
+    /// <summary>
+    /// 验证IPv4地址是否合法
+    /// </summary>
+    public class IsIPAddressAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                ErrorMessage = "IP地址不能为空!";
+                return false;
+            }
+            string email = value as string;
+            if (email.MatchInetAddress())
+            {
+                return true;
+            }
+            ErrorMessage = "IP地址格式不正确,请输入有效的IPv4地址";
+            return false;
+        }
+    }
+}

+ 26 - 0
Masuit.Tools/Validator/IsPhoneAttribute.cs

@@ -0,0 +1,26 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Validator
+{
+    /// <summary>
+    /// 验证手机号码是否合法
+    /// </summary>
+    public class IsPhoneAttribute : ValidationAttribute
+    {
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                ErrorMessage = "手机号码不能为空";
+                return false;
+            }
+            string phone = value as string;
+            if (phone.MatchPhoneNumber())
+            {
+                return true;
+            }
+            ErrorMessage = "手机号码格式不正确,请输入有效的大陆11位手机号码!";
+            return false;
+        }
+    }
+}

+ 31 - 0
Masuit.Tools/Validator/MaxValueAttribute.cs

@@ -0,0 +1,31 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Validator
+{
+    public class MaxValueAttribute : ValidationAttribute
+    {
+        private double MaxValue { get; }
+        public MaxValueAttribute(double value)
+        {
+            MaxValue = value;
+        }
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                return true;
+            }
+            var input = Convert.ToDouble(value);
+            return input <= MaxValue;
+        }
+
+        /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>
+        /// <param name="name">The name to include in the formatted message.</param>
+        /// <returns>An instance of the formatted error message.</returns>
+        public override string FormatErrorMessage(string name)
+        {
+            return base.FormatErrorMessage(name);
+        }
+    }
+}

+ 31 - 0
Masuit.Tools/Validator/MinValueAttribute.cs

@@ -0,0 +1,31 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace Masuit.Tools.Validator
+{
+    public class MinValueAttribute : ValidationAttribute
+    {
+        private double MinValue { get; set; }
+        public MinValueAttribute(double value)
+        {
+            MinValue = value;
+        }
+        public override bool IsValid(object value)
+        {
+            if (value is null)
+            {
+                return true;
+            }
+            var input = Convert.ToDouble(value);
+            return input > MinValue;
+        }
+
+        /// <summary>Applies formatting to an error message, based on the data field where the error occurred.</summary>
+        /// <param name="name">The name to include in the formatted message.</param>
+        /// <returns>An instance of the formatted error message.</returns>
+        public override string FormatErrorMessage(string name)
+        {
+            return base.FormatErrorMessage(name);
+        }
+    }
+}

+ 5 - 1
Test/App.config

@@ -7,7 +7,11 @@
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="DnsClient" publicKeyToken="4574bb5573c51424" culture="neutral" />
-        <bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.1.0" />
+        <bindingRedirect oldVersion="0.0.0.0-1.2.0.0" newVersion="1.2.0.0" />
+      </dependentAssembly>
+      <dependentAssembly>
+        <assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
       </dependentAssembly>
     </assemblyBinding>
   </runtime>

+ 4 - 5
Test/Test.csproj

@@ -32,8 +32,8 @@
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
-    <Reference Include="DnsClient, Version=1.1.1.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
-      <HintPath>..\packages\DnsClient.1.1.1\lib\net45\DnsClient.dll</HintPath>
+    <Reference Include="DnsClient, Version=1.2.0.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
+      <HintPath>..\packages\DnsClient.1.2.0\lib\net45\DnsClient.dll</HintPath>
     </Reference>
     <Reference Include="MongoDB.Bson, Version=2.7.0.0, Culture=neutral, processorArchitecture=MSIL">
       <HintPath>..\packages\MongoDB.Bson.2.7.0\lib\net45\MongoDB.Bson.dll</HintPath>
@@ -57,9 +57,8 @@
     <Reference Include="System.Core" />
     <Reference Include="System.IO.Compression" />
     <Reference Include="System.Management" />
-    <Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
-      <HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.0.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
-      <Private>True</Private>
+    <Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
+      <HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
     </Reference>
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />

+ 2 - 2
Test/packages.config

@@ -1,11 +1,11 @@
 <?xml version="1.0" encoding="utf-8"?>
 <packages>
-  <package id="DnsClient" version="1.1.1" targetFramework="net45" />
+  <package id="DnsClient" version="1.2.0" targetFramework="net45" />
   <package id="MongoDB.Bson" version="2.7.0" targetFramework="net45" />
   <package id="MongoDB.Driver" version="2.7.0" targetFramework="net45" />
   <package id="MongoDB.Driver.Core" version="2.7.0" targetFramework="net45" />
   <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net45" />
   <package id="StackExchange.Redis" version="1.2.6" targetFramework="net45" />
   <package id="System.Buffers" version="4.4.0" targetFramework="net45" />
-  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net45" />
+  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net45" />
 </packages>