.net 數據脫敏代碼實現


方案一:

DTO中處理:

        private string idNumber;
        /// <summary>
        /// 身份證號碼
        /// </summary>
        [Column("id_number")]
        public string IdNumber
        {
            get { return idNumber; }
            set { idNumber = SensitiveHelper.GetIdNumber(value); }
        }

 

方案二:

業務層處理:

核心方法如下:

        /// <summary>
        /// 根據輸入參數T中的特性,對屬性進行敏感數據處理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<T> GetSensitiveResult<T>(List<T> source)
        {
            if (!SysConfigHelper.Instance.GetBool("Para"))
            {
                return source;
            }

            List<PropertyInfo> props = GetPropertyInfos<SensitiveAttribute>(typeof(T));
            
            if (props.Count() > 0)
            {
                source.ForEach(r =>
                {
                    foreach (var prop in props)
                    {
                        var p = prop.GetCustomAttributes(true).OfType<SensitiveAttribute>().FirstOrDefault();
                        var accesor = new PropertyAccessor(typeof(T), prop.Name);
                        var value = accesor.Get(r)?.ToString();

                        switch (p.SensitiveType)
                        {
                            case SensitiveType.IdNumber:
                                accesor.Set(r, GetSensitiveIdNumber(value));
                                break;
                            case SensitiveType.Name:
                                accesor.Set(r, GetSensitiveName(value));
                                break;
                            default:
                                break;
                        }
                    }

                });
            }
            return source;
        }

        /// <summary>
        /// 姓名敏感處理
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static string GetSensitiveName(string fullName)
        {if (string.IsNullOrEmpty(fullName))
            {
                return "";
            }

            string familyName = fullName.Substring(0, 1);


            return familyName.PadRight(fullName.Length - 1, '*');
        }

        /// <summary>
        /// 證件號碼敏感處理
        /// </summary>
        /// <param name="idNumber"></param>
        /// <returns></returns>
        public static string GetSensitiveIdNumber(string idNumber)
        {if (string.IsNullOrEmpty(idNumber))
            {
                return "";
            }

            string number = idNumber.Substring(0, 6);

            return number.PadRight(idNumber.Length - 6, '*');
        }

在第一個方法中的類型T中,需要對要敏感處理的屬性增加特性Attribute

public class SensitiveAttribute: Attribute
    {
        #region Fields
        public SensitiveType SensitiveType { get; set; }
        #endregion

        #region Constructors and Destructors

        public SensitiveAttribute()
        {

        }

        public SensitiveAttribute(SensitiveType type)
        {
            this.SensitiveType = type;
        }
        #endregion

        #region Public Methods and Operators

        #endregion
    }

    public enum SensitiveType
    {
        Name,
        IdNumber
    }

在DTO中使用:

        [Sensitive(SensitiveType.Name)]
        public string BillPatientName { get; set; }

 

兩個方案各有利弊:

方案一:

優點:性能最好,不需要再循環對數據進行處理。

缺點:不推薦在DTO中的get,set方法中寫邏輯。

 

方案二:

優點:方法簡單,各司其職

缺點:性能不高。

 

最終我們選擇的方案一,希望有高手指點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM