Пример #1
0
        /// <summary>
        /// Enables string encryption on this model using an encryption provider.
        /// </summary>
        /// <param name="modelBuilder">Current <see cref="ModelBuilder"/> instance.</param>
        /// <param name="encryptionProvider">Encryption provider.</param>
        public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProvider encryptionProvider)
        {
            if (modelBuilder is null)
            {
                throw new ArgumentNullException(nameof(modelBuilder), "The given model builder cannot be null");
            }

            if (encryptionProvider is null)
            {
                throw new ArgumentNullException(nameof(encryptionProvider), "Cannot initialize encryption with a null provider.");
            }

            var encryptionConverter = new EncryptionConverter(encryptionProvider);

            foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
            {
                foreach (IMutableProperty property in entityType.GetProperties())
                {
                    if (property.ClrType == typeof(string) && !IsDiscriminator(property))
                    {
                        object[] attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptedAttribute), false);

                        if (attributes.Any())
                        {
                            property.SetValueConverter(encryptionConverter);
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Enables string encryption on this model using an encryption provider.
        /// </summary>
        /// <param name="modelBuilder">Current <see cref="ModelBuilder"/> instance.</param>
        /// <param name="encryptionProvider">Encryption provider.</param>
        public static void UseEncryption(this ModelBuilder modelBuilder, IEncryptionProvider encryptionProvider)
        {
            if (encryptionProvider == null)
            {
                return;
            }

            var encryptionConverter = new EncryptionConverter(encryptionProvider);

            foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
            {
                foreach (IMutableProperty property in entityType.GetProperties())
                {
                    if (property.ClrType == typeof(string) && !IsDiscriminator(property))
                    {
                        object[] attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptedAttribute), false);

                        if (attributes.Any())
                        {
                            property.SetValueConverter(encryptionConverter);
                        }
                    }
                }
            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var encryptionProvider  = new AesEncryptionProvider(ApplicationSettings.Database.EncryptionKey);
            var encryptionConverter = new EncryptionConverter(encryptionProvider);

            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                foreach (var property in entityType.GetProperties())
                {
                    if (property.ClrType != typeof(string) || IsDiscriminator(property))
                    {
                        continue;
                    }

                    var attributes = property.PropertyInfo.GetCustomAttributes(typeof(EncryptedAttribute), false);
                    if (attributes.Any())
                    {
                        property.SetValueConverter(encryptionConverter);
                    }
                }
            }
        }