I am currently trying to develop an API (running on Azure) that can access a DB but I don't want to have the connection string in clear text into my appsetting.json file for obvious security reasons and using Keyvault instead.
So I have already created the ressource group, the DB, the API Management Service, the Keyvault in which I already created my secret that contains the connection string my API requiers. In the Keyvault's access policy I have already added an entry for my API and gave it the permissions to get and list the secrets. Here is a screenshot.
Keyvault permission (sorry for the link, I am not allowed to embed an image yet)
In my app I have all the models and controllers I need.
Now my problem is, how, from my API, am I able to connect to my DB using this secret ?
my Program.CreateHostBuilder class look like this
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(async (context, builder) =>
{
var client = new SecretClient(
new Uri("https://example.vault.azure.net/"),
new DefaultAzureCredential());
KeyVaultSecret secret = await client.GetSecretAsync("ConnectionString");
string authenticationKey = secret.Value;
}
).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
What do I have to do from there for my app to connect to the DB ?
Thank you for reading ! :)