2

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.

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143

2 Answers2

4

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

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • Thanks @MartinCostello for your response. I have the updated the code : 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. Any help on this request is much appreciated – santosh kumar patro Apr 17 '23 at 15:56
1

Do I have the same swiggle with you under keyVaultURL ? If so then new Uri(uriString: keyVaultURL ?? "". Hope I misunderstood your issue.

enter image description here

Any way I just want to share the official document about using azure key vault in asp.net core and the sample code.

enter image description here

Tiny Wang
  • 10,423
  • 1
  • 11
  • 29