I'm updating a .NET 6 Blazor Server app that uses the .AddAzureKeyVault()
extension method to add an Azure KeyVault configuration provider from the old Microsoft.Extensions.Configuration.AzureKeyVault
over to the recommended Azure.Extensions.AspNetCore.Configuration.Secrets
package that uses the new SDK.
The complication I have is that the request goes through a network proxy. The current working version I have that uses the old SDK is this:
using Microsoft.Extensions.Configuration.AzureKeyVault; // 3.1.22
using Microsoft.Azure.Services.AppAuthentication; // 1.6.2
using Microsoft.Azure.KeyVault; // 3.0.5 (this needs to be version 3.0.0 or greater)
var builder = WebApplication.CreateBuilder(args);
var webProxy = new WebProxy(new Uri("{proxy_url}")) {
Credentials = CredentialCache.DefaultNetworkCredentials
};
var httpClient = new HttpClient(new HttpClientHandler {
Proxy = webProxy, SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
});
var authenticationCallback = new KeyVaultClient.AuthenticationCallback(
new AzureServiceTokenProvider().KeyVaultTokenCallback);
var keyVaultClient = new KeyVaultClient(authenticationCallback, httpClient);
builder.Configuration
.AddAzureKeyVault("{keyvault_url}", keyVaultClient, new DefaultKeyVaultSecretManager());
var output = builder.Configuration
.GetSection("ApplicationInsights:InstrumentationKey").Value; // successfully retrieves value
With the new SDK I attempted to pass the proxy into the HttpClientTransport
class but recieve a "The proxy tunnel request to proxy '{proxy_url}' failed with status code '407'." exception:
using Azure.Identity; // 1.5.0
using Azure.Security.KeyVault.Secrets; // 1.2.1
var builder = WebApplication.CreateBuilder(args);
var webProxy = new WebProxy(new Uri("{proxy_url}")) {
Credentials = CredentialCache.DefaultNetworkCredentials
};
var httpClient = new HttpClient(new HttpClientHandler {
Proxy = webProxy, SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
});
var azureCredentialOpts = new DefaultAzureCredentialOptions {
Transport = new HttpClientTransport(httpClient)
};
var secretClient = new SecretClient(new Uri("{keyvault_url}"),
new DefaultAzureCredential(azureCredentialOpts));
builder.Configuration
.AddAzureKeyVault(secretClient, new AzureKeyVaultConfigurationOptions());
// throws request to proxy failed with status code '407'
var output = builder.Configuration
.GetSection("ApplicationInsights:InstrumentationKey").Value;
I could find any mention of how to do this in the Microsoft docs and any examples I find use the old SDK. I did find this related question here - Azure .NET v4 SDK Proxy Configuration in .NET Framework, but the proposed solution never worked for me.
Only other thing to note was I did get this same '407' exception with the old SDK when using the Microsoft.Extensions.Configuration.AzureKeyVault
package and I had to explicitly upgrade the Microsoft.Azure.KeyVault
package to a version over 3.0.0 to get it to work, so not sure if this may be related (potentially the new SDK doesn't support the auth with the network proxy?...)
Would anyone know how I could use the Azure.Extensions.AspNetCore.Configuration.Secrets
package through a proxy?