question

DaveVs-3498 avatar image
0 Votes"
DaveVs-3498 asked ryanchill answered

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

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-webappsazure-managed-identityazure-webapps-authentication
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered

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>());


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.