I want the azure functions to use the connection string that is stored in azure key vault, not from application settings in azure portal.
I have stored the secret in azure portal and fetching it using the below code in Startup.cs
file, but it is not working.
Below is code piece I have tried:
Startup.cs
public override void Configure(IFunctionsHostBuilder builder)
{
// Reading connection string from app settings, (but I don't want this method)
// var configuration = Environment.GetEnvironmentVariable("SQL:ConnectionString");
var configuration = AzureKeyVault.GetSecretKey();
builder.Services.AddHttpClient();
Server.Module.Load(builder.Services, configuration);
}
AzureKeyVault Class
public class AzureKeyVault
{
public static String GetSecretKey()
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVault = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secretKey = Task.Run(async () => await keyVault.GetSecretAsync("https://somesecreturl.azure.net/", "mysecretkeyname")).Result;
var connectionString = secretKey.Value;
return connectionString;
}
}
The above code works fine in local, but after deploying it is not picking the connection string from azure key vault (for which I have written code), but I am getting an error Function host is not running
When I add connection string in application settings, function app is working fine with Environment.GetEnvironmentVariable("SQL:ConnectionString")
I am a newbie in azure. Azure functions are designed in a way that it can able to read the connection string only from application settings alone?
Any help would be highly appreciated. Thanks!