Skip to content

Commit b56a363

Browse files
committed
Update samples
1 parent 9a52243 commit b56a363

File tree

6 files changed

+71
-31
lines changed

6 files changed

+71
-31
lines changed

samples/AesSample.Fluent/DatabaseContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2626
userEntityBuilder.Property(x => x.FirstName).IsRequired();
2727
userEntityBuilder.Property(x => x.LastName).IsRequired();
2828
userEntityBuilder.Property(x => x.Email).IsRequired().IsEncrypted();
29+
userEntityBuilder.Property(x => x.Notes).IsRequired().HasColumnType("BLOB").IsEncrypted(StorageFormat.Binary);
2930
userEntityBuilder.Property(x => x.EncryptedData).IsRequired().IsEncrypted();
3031
userEntityBuilder.Property(x => x.EncryptedDataAsString).IsRequired().HasColumnType("TEXT").IsEncrypted(StorageFormat.Base64);
3132

32-
modelBuilder.UseEncryption(_encryptionProvider);
33+
if (_encryptionProvider is not null)
34+
{
35+
modelBuilder.UseEncryption(_encryptionProvider);
36+
}
3337

3438
base.OnModelCreating(modelBuilder);
3539
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace AesSample.Fluent;
4+
5+
public class EncryptedDatabaseContext : DatabaseContext
6+
{
7+
public EncryptedDatabaseContext(DbContextOptions<DatabaseContext> options)
8+
: base(options, null)
9+
{
10+
}
11+
}

samples/AesSample.Fluent/Program.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,38 @@ static void Main(string[] args)
2323
byte[] encryptionIV = keyInfo.IV;
2424
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV);
2525

26-
using var context = new DatabaseContext(options, encryptionProvider);
27-
context.Database.EnsureCreated();
28-
29-
var user = new UserEntity
26+
using (var context = new DatabaseContext(options, encryptionProvider))
3027
{
31-
FirstName = "John",
32-
LastName = "Doe",
33-
Email = "john@doe.com",
34-
EncryptedData = new byte[2] { 1, 2 },
35-
EncryptedDataAsString = new byte[2] { 3, 4 }
36-
};
28+
context.Database.EnsureCreated();
29+
30+
var user = new UserEntity
31+
{
32+
FirstName = "John",
33+
LastName = "Doe",
34+
Email = "john@doe.com",
35+
Notes = "Hello world!",
36+
EncryptedData = new byte[2] { 1, 2 },
37+
EncryptedDataAsString = new byte[2] { 3, 4 }
38+
};
39+
40+
context.Users.Add(user);
41+
context.SaveChanges();
3742

38-
context.Users.Add(user);
39-
context.SaveChanges();
43+
Console.WriteLine($"Users count: {context.Users.Count()}");
44+
}
4045

41-
Console.WriteLine($"Users count: {context.Users.Count()}");
46+
using (var context = new EncryptedDatabaseContext(options))
47+
{
48+
UserEntity user = context.Users.First();
49+
50+
Console.WriteLine($"Encrypted User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
51+
}
4252

43-
user = context.Users.First();
53+
using (var context = new DatabaseContext(options, encryptionProvider))
54+
{
55+
UserEntity user = context.Users.First();
4456

45-
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
57+
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
58+
}
4659
}
4760
}

samples/AesSample.Fluent/UserEntity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class UserEntity
1212

1313
public string Email { get; set; }
1414

15+
public string Notes { get; set; }
16+
1517
public byte[] EncryptedData { get; set; }
1618

1719
public byte[] EncryptedDataAsString { get; set; }

samples/AesSample/Program.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,31 @@ static void Main()
2323
byte[] encryptionIV = keyInfo.IV;
2424
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV);
2525

26-
using var context = new DatabaseContext(options, encryptionProvider);
27-
context.Database.EnsureCreated();
28-
29-
var user = new UserEntity
26+
using (var context = new DatabaseContext(options, encryptionProvider))
3027
{
31-
FirstName = "John",
32-
LastName = "Doe",
33-
Email = "john@doe.com",
34-
EncryptedData = new byte[2] { 1, 2 },
35-
EncryptedDataAsString = new byte[2] { 3, 4 }
36-
};
28+
context.Database.EnsureCreated();
29+
30+
var user = new UserEntity
31+
{
32+
FirstName = "John",
33+
LastName = "Doe",
34+
Email = "john@doe.com",
35+
Notes = "Hello world!",
36+
EncryptedData = new byte[2] { 1, 2 },
37+
EncryptedDataAsString = new byte[2] { 3, 4 }
38+
};
3739

38-
context.Users.Add(user);
39-
context.SaveChanges();
40+
context.Users.Add(user);
41+
context.SaveChanges();
4042

41-
Console.WriteLine($"Users count: {context.Users.Count()}");
43+
Console.WriteLine($"Users count: {context.Users.Count()}");
44+
}
4245

43-
user = context.Users.First();
46+
using (var context = new DatabaseContext(options, encryptionProvider))
47+
{
48+
UserEntity user = context.Users.First();
4449

45-
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}");
50+
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})");
51+
}
4652
}
4753
}

samples/AesSample/UserEntity.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class UserEntity
2020
[Encrypted]
2121
public string Email { get; set; }
2222

23+
[Required]
24+
[Encrypted(StorageFormat.Binary)]
25+
public string Notes { get; set; }
26+
2327
[Required]
2428
[Encrypted]
2529
public byte[] EncryptedData { get; set; }

0 commit comments

Comments
 (0)