Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. [TestInitialize]
  2. public void SetupTest()
  3. {
  4. CreateKeyvalut();
  5. }
  6.  
  7. public async void CreateKeyvalut()
  8. {
  9. try
  10. {
  11. IKeyVaultClient keyVaultClient = GetKeyVaultClient(_clientId, _certificateThumbprint);
  12.  
  13. var password = await GetSecretValueAsync(_secretIdentifier, keyVaultClient);
  14. }
  15. catch (Exception ex)
  16. {
  17. string errorMessage = $"[KeyVault] Error occurred when trying to connect Key Vault. Exception: {ex}";
  18. Trace.TraceWarning(errorMessage);
  19.  
  20. throw;
  21. }
  22. }
  23. public static IKeyVaultClient GetKeyVaultClient(string clientId, string certificateThumbprint) {
  24. return new KeyVaultClient(AuthenticationCallback(clientId, certificateThumbprint));
  25. }
  26.  
  27. public static KeyVaultClient.AuthenticationCallback AuthenticationCallback(string clientId, string certificateThumbprint)
  28. {
  29. return async (authority, resource, scope) =>
  30. {
  31. X509Certificate2 certificate = GetCertificate(certificateThumbprint);
  32. var context = new AuthenticationContext(authority);
  33. var clientCredentials = new ClientAssertionCertificate(clientId, certificate);
  34. AuthenticationResult result = await context.AcquireTokenAsync(resource, clientCredentials).ConfigureAwait(false);
  35. return result.AccessToken;
  36. };
  37. }
  38.  
  39. public static async Task<string> GetSecretValueAsync(string secretIdentifier, IKeyVaultClient keyVaultClient)
  40. {
  41. var secretTask = await keyVaultClient.GetSecretAsync(secretIdentifier);
  42. return secretTask.Value;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement