How to access TenantId, ClientId, Secret etc.. in C# Dotnet core from Azure.Identity DefaultAzureCredentials?

DaveVs 26 Reputation points
2021-07-29T04:37:00.937+00:00

Hi
I have an App Service web app, with a System Managed Identity.

I need to access individually the tenant id, client id and secret etc from within my code .. so I can pass them to a 3rd part library (mongodb client side field level encryption).

Is there any way to access these variables from within my code when using Azure.Identity library or are they stored in App service environment variables?

Thanks!

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,767 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,753 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 28,106 Reputation points Microsoft Employee
    2022-03-22T04:36:27.5+00:00

    You can add your values to your appsettings.json file like so or store them as Application Settings under App Service | Configuration > General Settings and access them with the following code in your StartUp.cs

    public IConfiguration Configuration { get; }
    ....
    var aad = Configuration.GetSection("AzureAd");
    

    You don't necessarily need Azure.Identity if all you're doing is pulling the values from within the app. If you're using something like App Config or Key Vault, then you'll need that API to utilize DefaultAzureCredential() and get the values.

    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
             webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
             {
                 configuration = config.Build();
                 var credentials = new DefaultAzureCredential();
                 config.AddAzureAppConfiguration(options =>
                 {
                     options.Connect(new Uri(configuration["AppConfigEndpoint"]), credentials);
                 });
             }).UseStartup<Startup>());
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.