Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.8k

The name does not exist in the current context

Aug 26 2019 9:25 AM
Hi team
 
Can anyone help with this logic error, "the name kvClient does not exist in the current context". Here is my code below. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Configuration;  
  6. using System.IO;  
  7. using System.Threading.Tasks;  
  8. using Microsoft.IdentityModel.Clients.ActiveDirectory;  
  9. using Microsoft.Azure;  
  10. using Microsoft.Azure.KeyVault;  
  11. using Microsoft.Azure.Storage;  
  12. using Microsoft.Azure.Storage.Auth;  
  13. using Microsoft.Azure.Storage.Blob.Protocol;  
  14. using Microsoft.Azure.Storage.Blob;  
  15. using System.Threading;  
  16. using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;  
  17. using Microsoft.Azure.Management.ResourceManager.Fluent;  
  18. namespace EncryptionApplication  
  19. {  
  20. class Program  
  21. {  
  22. static void Main(string[] args)  
  23. {  
  24. Program p = new Program();  
  25. string secretName = "MySecret10";  
  26. string kvURL = "https://entsavmvault.vault.azure.net/";  
  27. //  
  28. string clientId = Environment.GetEnvironmentVariable("akvClientId");  
  29. string clientSecret = Environment.GetEnvironmentVariable("akvClientSecret");  
  30. string tenantId = Environment.GetEnvironmentVariable("akvTenantId");  
  31. string subscription = Environment.GetEnvironmentVariable("akvSubscription");  
  32. AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud).WithDefaultSubscription(subscription);  
  33. KeyVaultClient keyVaultClient = new KeyVaultClient(async (authority, resource, scope) => {  
  34. var adCredential = new ClientCredential(clientId, clientSecret);  
  35. var authenticationContext = new AuthenticationContext(authority, null);  
  36. return (await authenticationContext.AcquireTokenAsync(resource,adCredential)).AccessToken;  
  37. Console.WriteLine("Input the value of your secret");  
  38. string secretValue = Console.ReadLine();  
  39. Console.WriteLine("Your secret is '" + secretValue + "'.");  
  40. Console.WriteLine("Saving the value of your secret to your key vault");  
  41. var result = p.SetSecret(kvClient, kvURL, secretName, secretValue);  
  42. System.Threading.Thread.Sleep(5000);  
  43. Console.WriteLine("done");  
  44. Console.WriteLine("Forgetting your secret");  
  45. secretValue = "";  
  46. Console.WriteLine("Your secret is ''" + secretValue);  
  47. Console.WriteLine("Retrieving your secret from key vault");  
  48. var fetchedSecret = p.GetSecret(kvClient,kvURL,secretName,secretValue);  
  49. secretValue = fetchedSecret.Result;  
  50. Console.WriteLine("Your secret is" + secretValue);  
  51. });  
  52. }  
  53. public async Task GetSecret(KeyVaultClient kvClient, string kvURL, string secretName)  
  54. {  
  55. var keyvaultSecret = await kvClient.GetSecretAsync($"{kvURL}/secrets/{secretName}").ConfigureAwait(false);  
  56. return keyvaultSecret.Value;  
  57. }  
  58. public async Task SetSecret(KeyVaultClient kvClient, string kvURL, string secretName, string secretValue)  
  59. {  
  60. await kvClient.SetSecretAsync($"{kvURL}", secretName, secretValue);  
  61. return true;  
  62. }  
  63. }  
  64. }  
What am i missing?

Answers (2)