Accessing HttpContext in Program.cs

91 views Asked by At

I am trying to build a multi-tenant application, where based on the user email, they will be mapped to different resources (database, redis, blob storage and application insights). The details of connection string will be available in separate key vaults. one key vault per tenant.

So when the API is called, based on the logged in users email, we should call a catalog service and fetch the key vault details and load it using AddAzureKeyVault. Is this possible. I am not able to access HttpContext when the web application is built. It is available only later. are they any other options?

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

//Get Keyvault by calling external API and passing the JWT token from header
var keyVault = GetKeyVaultFromExternalAPI();
builder.Configuration.AddAzureKeyVault(new Uri($"https://{keyVault}.vault.azure.net"), new DefaultAzureCredential());

I am thinking of using a middleware, which will update the configuration, but not sure, how it can update the redis connection string or Application Insights connection.

0

There are 0 answers