Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [TestInitialize]
- public void SetupTest()
- {
- CreateKeyvalut();
- }
- public async void CreateKeyvalut()
- {
- try
- {
- IKeyVaultClient keyVaultClient = GetKeyVaultClient(_clientId, _certificateThumbprint);
- var password = await GetSecretValueAsync(_secretIdentifier, keyVaultClient);
- }
- catch (Exception ex)
- {
- string errorMessage = $"[KeyVault] Error occurred when trying to connect Key Vault. Exception: {ex}";
- Trace.TraceWarning(errorMessage);
- throw;
- }
- }
- public static IKeyVaultClient GetKeyVaultClient(string clientId, string certificateThumbprint) {
- return new KeyVaultClient(AuthenticationCallback(clientId, certificateThumbprint));
- }
- public static KeyVaultClient.AuthenticationCallback AuthenticationCallback(string clientId, string certificateThumbprint)
- {
- return async (authority, resource, scope) =>
- {
- X509Certificate2 certificate = GetCertificate(certificateThumbprint);
- var context = new AuthenticationContext(authority);
- var clientCredentials = new ClientAssertionCertificate(clientId, certificate);
- AuthenticationResult result = await context.AcquireTokenAsync(resource, clientCredentials).ConfigureAwait(false);
- return result.AccessToken;
- };
- }
- public static async Task<string> GetSecretValueAsync(string secretIdentifier, IKeyVaultClient keyVaultClient)
- {
- var secretTask = await keyVaultClient.GetSecretAsync(secretIdentifier);
- return secretTask.Value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement