0

Error message::

The SSL connection could not be established, see inner exception.'SocketException: An established connection was aborted by the software in your host machine.

This is the code used to connect to Azure Keyvault from local machine.

public static async Task PrivacyAsync()
{
        string Client_Id = "xxxxxxxx";
        string Client_Secret = "xxxxxx";
        var kvUri = "xxxx";

        var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(
            async (string auth, string res, string scope) =>
            {
                var authcontext = new AuthenticationContext(auth);
                var credential = new ClientCredential(Client_Id, Client_Secret);
                AuthenticationResult result = await authcontext.AcquireTokenAsync(res, credential);

                if (res == null)
                {
                    throw new InvalidOperationException("token failed");

                   
                }
                return result.AccessToken;
            }
            ));

        var secret = await client.GetSecretAsync(kvUri, "xxxxxx");
        Console.WriteLine("secret success full read"+secret.Value);
        Console.ReadLine();
    
        //var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
        //await client.SetSecretAsync("test0311", "hello");
        
}

Expected result:

Blob storage connection string value stored into Keyvault secret. I need to fetch blob storage connection string value from Keyvault secret using C# code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0
  • Make sure you have given necessary permissions to access the Azure KeyVault Secrets.
  • In Azure Active Directory => App Registration => Authentication, check the implicit options.

enter image description here

  • In Access Policies, provide the below permissions and select the same registered App.

enter image description here

enter image description here

The code which you have provided is Obsolete(old).

When I tried with your code, I got the below warning.

enter image description here

  • I have added [Obselete].

enter image description here

  • I have tried with the below 2 codes and it worked for me.

Code 1:

    string Client_Id = "**********";
    string Client_Secret = "**********";
    string tenantID = "**********";
    var kvUri = "https://mykeyvault.vault.azure.net/";


    string secretName = "BlobConnection";

    var credential = new ClientSecretCredential(tenantID, Client_Id, Client_Secret);
    var client1 = new SecretClient(new Uri(kvUri), credential);

    KeyVaultSecret secret = client1.GetSecret(secretName);

    Console.WriteLine("secret success full read" + secret.Value);

OR

Install the latest versions of the below NuGet Packages.

 Azure.Security.KeyVault.Secrets;
 Azure.Identity

Code 2 :

public static async Task PrivacyAsync()
 {              
    var kvUri = "https://mykeyvault.vault.azure.net/";      
    var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
    var secret = await client.GetSecretAsync("BlobConnection");
    Console.WriteLine("secret success full read" + secret.Value.Value);
    Console.ReadLine();
 }

enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9