Search code examples
c#asp.net-corevisual-studio-2022azure-keyvault.net-7.0

suggest using webapplicationbuilder.configuration instead of configureappconfiguration


I am trying to read value from appsettings.Development.json file using Managed Identity for.NET 7 asp.net core web api application using Visual Studio 2022 (Enterprise Edition)

Here goes the code for Program.cs :

var builder = WebApplication.CreateBuilder(args);
var config = builder.Configuration;

// Add AzureKeyVault

    builder.Host.ConfigureAppConfiguration((context, appConfig) =>
    {
        var objBuiltConfig = appConfig.Build();
        var userAssignedClientId = objBuiltConfig[AppKeys.UserAssignedClientId];
        var objSecretClient = new SecretClient(new Uri(objBuiltConfig[AppKeys.KeyVaultURL]),
            new DefaultAzureCredential(new DefaultAzureCredentialOptions
            {
                ManagedIdentityClientId = userAssignedClientId
            }));
        config.AddAzureKeyVault(objSecretClient, new KeyVaultSecretManager());
    });

In the code editor I am seeing the warning :

suggest using webapplicationbuilder.configuration instead of configureappconfiguration

Even on debugging I am not getting the data.

Can anyone help me here with some code sample which will serve as a reference for my implementation

====================update======================

var userAssignedClientId = config[AppKeys.UserAssignedClientId]; 
var keyVaultURL = config[AppKeys.KeyVaultURL]; 

builder.Configuration.AddAzureKeyVault(new Uri(uriString: keyVaultURL), new 
     DefaultAzureCredential(new DefaultAzureCredentialOptions { 
          ManagedIdentityClientId = userAssignedClientId })); 

But now I see swiggle with keyVaultURL within the new Uri(uriString: keyVaultURL). I wanted to know why it is coming.


Solution

  • Instead of this:

    builder.Host.ConfigureAppConfiguration((context, appConfig) =>
    {
        config.AddAzureKeyVault(...);
    });
    

    Change to this:

    builder.Configuration.AddAzureKeyVault(...);
    

    Here's an example of this sort of approach for an application I maintain: github.com/martincostello/SignInWithAppleSample