Access Azure key vault via c# console application

Wanda 1 Reputation point
2022-08-22T14:10:23.153+00:00

Hello,

I am trying to access the Azure secret via a C# console application. It works when I am in the debug mode as I registered myself in the Azure policy. However, if I just run the "exe", I receive the following error. What should I register in order to let the machine or console app to run?

Azure.Identity.CredentialUnavailableException: DefaultAzureCredential failed to retrieve a token from the included credentials

Thanks in advance!

Azure Key Vault
Azure Key Vault
An Azure service that is used to manage and protect cryptographic keys and other secrets used by cloud apps and services.
1,135 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,303 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,926 Reputation points
    2022-08-22T16:56:30.44+00:00

    what authentication are you using to access the keyvault?

    connection string
    client secret
    msal


  2. Akshay-MSFT 16,126 Reputation points Microsoft Employee
    2022-09-08T11:07:49.763+00:00

    Hello @Wanda ,

    I was able to check through this and found that this is a known error while retrieving secret from key vault as per ref: https://techcommunity.microsoft.com/t5/iis-support-blog/defaultazurecredential-failed-to-retrieve-a-token/ba-p/3038734. Kindly follow the given actions :

    In order to solve this issue in a local machine:

    1. Add Active Directory app registration on Azure
    2. Create access policy for this app registration in Azure Key Vault settings
    3. Create environment variables for AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID (Reference: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/identity/Azure.Identity#environment-variables)

    Thanks,
    Akshay Kaushik

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments

  3. Wanda 1 Reputation point
    2022-09-12T11:53:13.67+00:00

    Hi Akshay,

    The challenge is I have AZURE_CLIENT_ID, AZURE_CLIENT_SECRET all in the Azure Key Vault since I want to save these variables in a secure way instead of having them in the environment variables. How safe is it to have the AZURE_CLIENT_SECRET in the environment variables? I initially have them all in the machine.config and decide to move them to Azure Key Vault.

    Please advise.