How to Pass AzureAdB2C Configurations to an Orchestrated Application in ASP.NET Core?

66 Views Asked by At

I am working on an .NET Core Aspire project where I am trying to pass AzureAdB2C configurations from my Aspire application's appsettings.json to an orchestrated application.

How can I successfully pass AzureAdB2C configurations from appsettings.json to my orchestrated application? Is there a best practice to ensure that configurations are correctly transferred between the main application and orchestrated applications? Any help or guidance would be greatly appreciated.

I am using the following approach to configure services and containers:

var builder = DistributedApplication.CreateBuilder(args);

var azureAdB2C = builder.Configuration["AzureAdB2C"];

var redis = builder.AddRedisContainer("redis");
var postgres = builder.AddPostgresContainer("postgres")
    .WithAnnotation(new ContainerImageAnnotation
    {
        Image = "ankane/pgvector",
        Tag = "latest",
    });

var tenantsDb = postgres.AddDatabase("TenantsDb");

// Services
var tenantsApi = builder.AddProject<Projects.Vexel_Tenants_Api>("vexel.tenants.api")
    .WithReference(redis)
    .WithReference(tenantsDb)
    .WithEnvironment("AzureAdB2C", azureAdB2C);

builder.Build().Run();

However, when trying to access the configurations in the tenant application, I am unable to retrieve the expected values:

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

var test1 = Environment.GetEnvironmentVariable("AzureAdB2C");
var test2 = builder.Configuration.GetSection("AzureAdB2C");

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApi(options =>
            {
                builder.Configuration.Bind("AzureAdB2C", options);
            },
    options => { builder.Configuration.Bind("AzureAdB2C", options); });

Here, test1 is returning null, and the sections of test2 come back empty.

1

There are 1 best solutions below

1
Victor On

For the moment, I created the following extension method:

public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> builder, IConfigurationSection section)
     where T : IResource
{
    foreach (var item in section.GetChildren())
    {
        _ = builder.WithEnvironment($"{section.Path}__{item.Key}", item.Value);
    }

    return builder;
}

And it should be used like this:

var azureAdB2C = builder.Configuration.GetSection("AzureAdB2C");
var tenantsApi = builder.AddProject<Projects.Vexel_Tenants_Api>("vexel.tenants.api")
    .WithEnvironment(azureAdB2C);