diff --git a/Falcon.SugarApi/DatabaseManager/DatabaseDefines.cs b/Falcon.SugarApi/DatabaseManager/DatabaseDefines.cs
index dcf1e66..ccb7afe 100644
--- a/Falcon.SugarApi/DatabaseManager/DatabaseDefines.cs
+++ b/Falcon.SugarApi/DatabaseManager/DatabaseDefines.cs
@@ -23,6 +23,10 @@ namespace Falcon.SugarApi.DatabaseManager
/// 链接字符串
///
public string ConnectionString { get; set; }
+ ///
+ /// 是否支持字段加密
+ ///
+ public bool IsEnableEncrypy { get; set; } = true;
}
}
diff --git a/Falcon.SugarApi/DatabaseManager/DbContextBaseEncryptExtend.cs b/Falcon.SugarApi/DatabaseManager/DbContextBaseEncryptExtend.cs
new file mode 100644
index 0000000..e94969a
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseManager/DbContextBaseEncryptExtend.cs
@@ -0,0 +1,39 @@
+using System.Reflection;
+
+namespace Falcon.SugarApi.DatabaseManager
+{
+ ///
+ /// 数据库上下文字段加密支持
+ ///
+ public static class DbContextBaseEncryptExtend
+ {
+ ///
+ /// 设置数据库上下文支持字段加密
+ ///
+ /// 数据库上下文
+ public static void SetEnableEncrypt(this DbContextBase dbContext) {
+ dbContext.Aop.DataExecuting = (value,entity) => {
+ var attr = entity.GetAttribute();
+ var enProvider = attr.EncryptType.Assembly.CreateInstance(attr.EncryptType.FullName) as IEncrypt;
+ if(attr == null) {
+ return;
+ }
+ var newVal = enProvider.Encrypt(value.ToString());
+ entity.SetValue(newVal);
+ };
+ dbContext.Aop.DataExecuted = (value,entity) => {
+ foreach(var col in entity.EntityColumnInfos) {
+ var pName = col.PropertyName;
+ var attr = col.PropertyInfo.GetCustomAttribute();
+ var enProvider = attr.EncryptType.Assembly.CreateInstance(attr.EncryptType.FullName) as IEncrypt;
+ if(attr == null) {
+ continue;
+ }
+ var newVal = enProvider.Decrypt(entity.GetValue(pName).ToString());
+ entity.SetValue(pName,newVal);
+ }
+ };
+
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseManager/EnableEncryptAttribute.cs b/Falcon.SugarApi/DatabaseManager/EnableEncryptAttribute.cs
new file mode 100644
index 0000000..0f52372
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseManager/EnableEncryptAttribute.cs
@@ -0,0 +1,28 @@
+using System;
+
+namespace Falcon.SugarApi.DatabaseManager
+{
+ ///
+ /// 允许对字段进行加密存储
+ ///
+ [AttributeUsage(AttributeTargets.Property,AllowMultiple = false,Inherited = false)]
+ public class EnableEncryptAttribute:Attribute
+ {
+ ///
+ /// 用于加密的类型
+ ///
+ public Type EncryptType { get; set; }
+
+ ///
+ /// 通过提供加密解密接口实现字段加密
+ ///
+ /// 实现加密解密的类型
+ /// 提供的类型不符合要求
+ public EnableEncryptAttribute(Type encryptType) {
+ if(!typeof(IEncrypt).IsAssignableFrom(encryptType)) {
+ throw new ArgumentException("encryptType must implement IEncrypt");
+ }
+ this.EncryptType = encryptType;
+ }
+ }
+}
diff --git a/Falcon.SugarApi/DatabaseManager/IEncrypt.cs b/Falcon.SugarApi/DatabaseManager/IEncrypt.cs
new file mode 100644
index 0000000..f80b571
--- /dev/null
+++ b/Falcon.SugarApi/DatabaseManager/IEncrypt.cs
@@ -0,0 +1,21 @@
+namespace Falcon.SugarApi.DatabaseManager
+{
+ ///
+ /// 字段加密解密方法
+ ///
+ public interface IEncrypt
+ {
+ ///
+ /// 加密。当存储字段值进入数据库时使用该方法进行加密
+ ///
+ /// 字段明文值
+ /// 字段密文
+ string Encrypt(string str);
+ ///
+ /// 解密。当读取字段内容时对数据库中的加密值进行解密
+ ///
+ /// 字段密文
+ /// 字段明文值
+ string Decrypt(string str);
+ }
+}
diff --git a/Falcon.SugarApi/Falcon.SugarApi.csproj b/Falcon.SugarApi/Falcon.SugarApi.csproj
index f76a915..344fdfc 100644
--- a/Falcon.SugarApi/Falcon.SugarApi.csproj
+++ b/Falcon.SugarApi/Falcon.SugarApi.csproj
@@ -8,7 +8,7 @@
bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml
true
true
- 2.11.0
+ 2.12.0