-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
47 lines (37 loc) · 1.59 KB
/
Copy pathProgram.cs
File metadata and controls
47 lines (37 loc) · 1.59 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
namespace AlwaysEncryptedKeyVaultIssue
{
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.SqlServer.Management.AlwaysEncrypted.AzureKeyVaultProvider;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
public class Program
{
private static ClientCredential clientCredential;
static void Main(string[] args)
{
InitializeAzureKeyVaultProvider();
}
public static void InitializeAzureKeyVaultProvider()
{
var clientId = Guid.NewGuid().ToString();
var clientSecret = "ITS_A_SECRET";
clientCredential = new ClientCredential(clientId, clientSecret);
var azureKeyVaultProvider = new SqlColumnEncryptionAzureKeyVaultProvider(GetToken);
var providers = new Dictionary<string, SqlColumnEncryptionKeyStoreProvider>()
{
{ SqlColumnEncryptionAzureKeyVaultProvider.ProviderName, azureKeyVaultProvider }
};
SqlConnection.RegisterColumnEncryptionKeyStoreProviders(providers);
}
public async static Task<string> GetToken(string authority, string resource, string scope)
{
var authContext = new AuthenticationContext(authority);
AuthenticationResult result = await authContext.AcquireTokenAsync(resource, clientCredential);
if (result == null)
throw new InvalidOperationException("Failed to obtain the access token");
return result.AccessToken;
}
}
}