How to migrate Microsoft.Extensions.Configuration.AzureKeyVault to MSAL

131 views Asked by At

We're trying to migrate a net6.0 console from ADAL to MSAL. After removing packages- Microsoft.Extensions.Configuration.AzureKeyVault and Microsoft.IdentityModel.Clients.ActiveDirectory, we're getting compile time errors on AzureServiceTokenProvider, KeyVaultClient and AddAzureKeyVault shown below. How should we replace the following block of code to provide the same functionality but using MSAL packages?

MyJobOption jobOption = Configuration.GetSection("MyJobOption").Get<MyJobOption>();
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
// The following line causes CS1061: 'ConfigurationBuilder' does not contain a definition for 'AddAzureKeyVault' without the Microsoft.Extensions.Configuration.AzureKeyVault package
Configuration = configBuilder.AddAzureKeyVault(
    new AzureKeyVaultConfigurationOptions()
    {
        Vault = jobOption.KeyVaultUrl,
        Client = keyVaultClient,
        Manager = new DefaultKeyVaultSecretManager()
    })
    .Build();
services.AddSingleton(Configuration);
1

There are 1 answers

0
sich On BEST ANSWER

The latest package for supporting Azure KeyVault configuration source is named Azure.Extensions.AspNetCore.Configuration.Secrets. Also, for authentication you will need the Azure.Identity package. Esure you installed these, and your code for KeyVault configuration will be no more complex than this:

configBuilder.AddAzureKeyVault(new Uri("<Vault URI>"), new DefaultAzureCredential());