Eastrall / EntityFrameworkCore.DataEncryption

A plugin for Microsoft.EntityFrameworkCore to add support of encrypted fields using built-in or custom encryption providers.
MIT License
326 stars 54 forks source link

How to use user defined encryption key? #41

Closed planyway closed 1 year ago

planyway commented 1 year ago

Everytime a user read/edit the database, he/she has to type a key he/she originally used to encrypt data. This typed key is going to overwrite appsettings.json's key, so ApplicationDbContext will use this typed key to encrypt/decrypt the data.

I can now get user's key from the razor page, and assign the key to HomeController's configuration (private readonly IConfiguration configuration) by using Httppost. Now I wonder how I can force ApplicationDbContext to re-decrypt the data it has, after the user enters a key? Because before the user enters the key, ApplicationDbContext should only managed to get garbled text or something weird, because the appsettings.json key must be a wrong key.

All I see now here are codes that only configure the AES key before the model is created, so I am finding a way to let the key reconfigure after the model is created.

Eastrall commented 1 year ago

EFCore model is cached when you access it the first time for performance reasons. But there is a way to that allows you to have "multiple" models at the same time, checkout this article: https://docs.microsoft.com/en-us/ef/core/modeling/dynamic-model