Open Closed

Override DB Client to use MongoDB CSFLE #7157


User avatar
0
aaguiar created

Hi, I'm trying to implement MongoDB CSFLE, I need a way to override the MongoDB settings and the MongoClient.

I created a class implementing AbpMongoDbContext could you please let me know how can I achieve that? One of the settings that I have to change is: mongoDBSettings.AutoEncryptionOptions

Database and Client are private set, so I can't override it.

  • ABP Framework version: 8.0.3
  • UI Type: Angular
  • Database System: MongoDB
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

7 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

    You can configure the AbpMongoDbContextOptions

    Configure<AbpMongoDbContextOptions>(options =>
    {
        options.MongoClientSettingsConfigurer = settings =>
        {
            settings.AutoEncryptionOptions = ....
        };
    });
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    aaguiar created

    To be able to build the AutoEncryptionOptions I need to fetch the collection to grab the encryption key, how can I achieve that in the DBModule if I don't have the MongoClient just yet?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    You can check this: https://stackoverflow.com/questions/68659436/how-to-encrypt-a-class-property-using-mongodb-csfle-in-dot-net-core

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    aaguiar created

    I'm not talking about the CSFLE implementation, that part is done, my question is how to override the MongoClient in the AbpMongoDbContext, because I need to fetch the keys from the DB, create the AutoEncryptionOptions and then override the client config

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    There are two ways:

    1:

    https://support.abp.io/QA/Questions/7157/Override-DB-Client-to-use-MongoDB-CSFLE#answer-3a126877-4681-cdaa-d7a7-13856681c892

    2:

    Override the CreateMongoClient method of the UnitOfWorkMongoDbContextProvider

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    aaguiar created

    Found a solution which worked:

    public override void InitializeDatabase(IMongoDatabase database, IMongoClient client, IClientSessionHandle sessionHandle)
    { 
        var clientSettings = client.Settings.Clone();
    
        clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
    
        client = new MongoClient(clientSettings);
        database = client.GetDatabase(database.DatabaseNamespace.DatabaseName);
        base.InitializeDatabase(database, client, sessionHandle);
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Good to see you find a way

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.