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
Instead of this:
Change to this:
Here’s an example of this sort of approach for an application I maintain: github.com/martincostello/SignInWithAppleSample
Do I have the same swiggle with you under
keyVaultURL
? If so thennew Uri(uriString: keyVaultURL ?? ""
. Hope I misunderstood your issue.Any way I just want to share the official document about using azure key vault in asp.net core and the sample code.