You can supply your own configure provider or use the supplied one.
https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-8.0
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I’d like to modify the Program.cs for a .NET Core to get a database connection string (DBConnstrVal) from an Azure Key Vault. Currently, this comes from AppSettings.JSON, and is available to the models and controllers in the same ways as the rest of the data in the AppConfig. The app is running locally & will be deployed to a server.
This is the sample code for getting the value DBConnstrKey from the Azure Key Vault at keyVaultUri from a console app run locally
var client = new SecretClient(new Uri(keyVaultUri), new DefaultAzureCredential());
var secret = await client.GetSecretAsync(“DBConnstrKey”);
The code snippet for the Program.cs below shows how I am loading AppSettings.JSON values & adding role policies. ( Other builder.services statements have been left out for clarity ) .
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme).AddNegotiate();
var Admins = builder.Configuration["AppConfig:Admins"];
var Editors = builder.Configuration["AppConfig:Editors"];
var Readers = builder.Configuration["AppConfig:Readers"];
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Admins", policy => policy.RequireRole(Admins));
options.AddPolicy("Editors", policy => policy.RequireRole(Editors));
options.AddPolicy("Readers", policy => policy.RequireRole(Readers));
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.Configure<AppConfig>(builder.Configuration.GetSection("AppConfig"));
var app = builder.Build();
This is the AppConfig class
public class AppConfig
{
public string AppTitle { get; set; }
public string DBConnstrVal { get; set; }
public string Admins { get; set; }
public string Editors { get; set; }
public string Readers { get; set; }
public string KeyVaultUrl { get; set; }
public string SecretConnKey { get; set; }
}
How do I modify Program.cs to use the connection string from the Azure Key Vault and make it available to the other models & controllers in the MVC app, like the AppSettings.JSON values are currently ?
Thanks in advance
( Apologies if posted twice, the previous attempt to post ended up as a 404 )
You can supply your own configure provider or use the supplied one.
https://learn.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-8.0