Search code examples
c#asp.net-mvcazure-ad-b2cazure-keyvaultmicrosoft-entra-id

Store Azure AzureADB2C object in ASP .NET MVC application as a Key Vault secret


I have AzureADB2C object in my ASP.NET MVC application stored in the appsettings.json file.

I want to store Azure AzureADB2C object as a Key Vault secret and then access it from Program.cs like so:

builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureADB2C"));

How can I do it?

I implemented cloud authentication in my app using the following instruction


Solution

  • Based on this answer I was able to do the following changes

    In Program.cs before adding authentication with Microsoft identity platform, I pasted the following code:

    IConfiguration azureADB2C = builder.Configuration.GetSection("AzureADB2C");
    
    azureADB2C["TenantId"] = builder.Configuration.GetSection("TenantId").Value;
    azureADB2C["ClientId"] = builder.Configuration.GetSection("ClientId").Value;
    azureADB2C["ClientSecret"] = builder.Configuration.GetSection("ClientSecret").Value;
    

    In appsettings.json I left the corresponding values empty:

    "AzureAdB2C": {
      "TenantId": "",
      "ClientId": "",
      "ClientSecret": "",
      ...
    }