I am trying to use Mongodb Client side field level encryption (CSFLE) with Dot net core (v5.0).
I have it working using the Json Schema map as per docs, which uses BSONDocument types and I can define each field to encrypt (have followed this guide https://www.mongodb.com/developer/how-to/client-side-field-level-encryption-mongodb-csharp/)
I now need to apply this to individual properties on my C# Class models. Can I use an annotation or somehow tell MongoDb Driver to serialize my class property to the binary type required in the BsonDocument schema?
This works:
var schemaMap = $@"{{
properties: {{
SSN: {{
encrypt: {{
keyId: [{{
'$binary' : {{
'base64' : '{base64DataKeyId}',
'subType' : '04'
}}
}}],
bsonType: 'string',
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic'
}}
}}
}},
'bsonType': 'object'
}}";
var autoEncryptionSettings = new AutoEncryptionOptions(
keyVaultNamespace,
kmsProviders,
schemaMap: new Dictionary<string, BsonDocument>()
{
{ collectionNamespace.ToString(), BsonDocument.Parse(schemaMap) }
});
I need to somehow apply this to a C# class model like this:
public class TestModel
{
[MongoEncrypted?]
public string SSN { get; set; }
}
I use a basic repository pattern, and currently pass my Class models in to the Mongo driver like this
_mongoCollection = _mongoContext.GetCollection<TEntity>(collectionName);
How do I tell the Mongodb Driver to apply the Json Schema to my existing class properties? Is this possible?
Thank you.
no, it's not possible, json schema should be provided at the moment when you create a mongo client