locked
Need help in fetching secret from KeyVault using C# RRS feed

  • Question

  • Hi All,

    I used below script to fetch a secret from KeyVault. it worked for few days and suddenly started throwing below error. I have to admin that i have been making too many changes to my KeyVault policies to test few other things and in process, I might have broken this somehow. Please help me figure out what i need here. All i need to do is fetch a secret from AKV using C#. 

    Error Message:

    System.AggregateException
      HResult=0x80131500
      Message=One or more errors occurred. (Parameters: Connection String: [No connection string specified], Resource: https://vault.azure.net, Authority: https://login.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47. Exception Message: Tried the following 3 methods to get an access token, but none of them worked.

    using System;
    using System.Threading.Tasks;
    using Azure.Core;
    using Azure.Identity;
    using Microsoft.Azure.KeyVault;
    using Microsoft.Azure.KeyVault.Models;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    using Microsoft.Azure.Services.AppAuthentication;
    
    namespace FetchSecret
    {
        class Program
        {
            public static void Main(string[] args)
            {
                AzureServiceTokenProvider tokenProvider = new AzureServiceTokenProvider();
                KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(tokenProvider.KeyVaultTokenCallback));
                var credentail = keyVaultClient.GetSecretAsync("https://MYKEYVAULTNAME.vault.azure.net/secrets/MYSECRETNAME/").Result;
                var secret = credentail.Value.ToString();
                Console.WriteLine(secret);
    
            }
        }
    }


    Wednesday, May 20, 2020 5:55 AM

All replies

  • Are you trying to run your code in your local environment ? If yes, can you please check if you have selected the correct Account under "Azure Service Authentication" in your Visual Studio. (Go to Visual Studio and Tools > Options).

    When you run code in your local the AppAuthentication library uses your developer credentials to connect to run your local development environment and fetches tokens using Visual Studio, Azure CLI or Azure AD Authentication.  If none of options works then you get the error you have posted in your question. Please refer to Local development authentication for details.  

    Wednesday, May 20, 2020 8:22 PM
  • Please let me know if you find above reply useful. If yes, do click on 'Mark as answer' link in above reply. This will help other community members facing similar query to refer to this solution. Thanks.
    Tuesday, June 2, 2020 6:21 PM