skip to Main Content

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.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search