-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
110 lines (96 loc) · 4.06 KB
/
Copy pathProgram.cs
File metadata and controls
110 lines (96 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Azure.Identity;
using EfCore.EncryptedProperties.Extensions;
using EfCore.EncryptedProperties.Samples.AzureKeyVault;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
var configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
.AddUserSecrets<Program>(optional: true, reloadOnChange: false)
.Build();
var keyVaultKeyUri = new Uri(GetRequiredSetting(configuration, "AzureKeyVault:KeyUri"));
var credential = new ClientSecretCredential(
tenantId: GetRequiredSetting(configuration, "AzureKeyVault:TenantId"),
clientId: GetRequiredSetting(configuration, "AzureKeyVault:ClientId"),
clientSecret: GetRequiredSetting(configuration, "AzureKeyVault:ClientSecret"));
var services = new ServiceCollection();
services.AddEncryptedProperties(cfg =>
{
cfg.WithAzureKeyVaultRsaKeyProvider(keyVaultKeyUri, credential);
cfg.WithInMemoryKeyChain();
});
services.AddDbContext<SampleDbContext>((sp, options) =>
{
options.UseInMemoryDatabase("SampleDb");
options.UseEncryptedProperties(sp);
});
await using var serviceProvider = services.BuildServiceProvider();
// Insert a customer
var customerId = Guid.NewGuid();
await using (var scope = serviceProvider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<SampleDbContext>();
ctx.Customers.Add(new Customer
{
Id = customerId,
Name = "Alice",
Email = "alice@example.com",
DateOfBirth = new DateTime(1990, 5, 21),
SecretNotes = "This is a secret note about Alice.",
LoyaltyPoints = 1250
});
await ctx.SaveChangesAsync();
Console.WriteLine("Customer saved with encrypted Email, DateOfBirth, SecretNotes, and LoyaltyPoints.");
}
// Read back
await using (var scope = serviceProvider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<SampleDbContext>();
var customer = await ctx.Customers.FindAsync(customerId);
if (customer is null)
{
Console.WriteLine("Customer not found!");
return;
}
// DecryptOnRead: Email is already decrypted
Console.WriteLine($"Name: {customer.Name}");
Console.WriteLine($"Email (DecryptOnRead): {customer.Email}");
Console.WriteLine($"DateOfBirth (DecryptOnRead): {customer.DateOfBirth:yyyy-MM-dd}");
// Lazy: SecretNotes requires explicit decryption
var notes = await customer.SecretNotes.GetDecryptedValueAsync();
Console.WriteLine($"SecretNotes (Lazy): {notes}");
var points = await customer.LoyaltyPoints.GetDecryptedValueAsync();
Console.WriteLine($"LoyaltyPoints (Lazy): {points}");
}
// Update encrypted values
await using (var scope = serviceProvider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<SampleDbContext>();
var customer = await ctx.Customers.FindAsync(customerId);
customer!.Email = "alice.updated@example.com";
customer.DateOfBirth = new DateTime(1991, 6, 22);
customer.SecretNotes = "Updated secret note.";
customer.LoyaltyPoints = 1500;
await ctx.SaveChangesAsync();
Console.WriteLine("\nCustomer updated.");
}
// Verify update
await using (var scope = serviceProvider.CreateAsyncScope())
{
var ctx = scope.ServiceProvider.GetRequiredService<SampleDbContext>();
var customer = await ctx.Customers.FindAsync(customerId);
Console.WriteLine($"Updated Email: {customer!.Email}");
Console.WriteLine($"Updated DateOfBirth: {customer.DateOfBirth:yyyy-MM-dd}");
var notes = await customer.SecretNotes.GetDecryptedValueAsync();
Console.WriteLine($"Updated SecretNotes: {notes}");
var points = await customer.LoyaltyPoints.GetDecryptedValueAsync();
Console.WriteLine($"Updated LoyaltyPoints: {points}");
}
static string GetRequiredSetting(IConfiguration configuration, string key)
{
var value = configuration[key];
if (string.IsNullOrWhiteSpace(value))
throw new InvalidOperationException($"Required configuration value '{key}' is missing.");
return value;
}