我正在使用Azure KeyVault来获取Program.cs中的秘密:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
var builtConfig = config.Build();
config.AddAzureKeyVault(
$"https://{builtConfig["KeyVault:Name"]}.vault.azure.net/",
builtConfig["KeyVault:ClientId"],
builtConfig["KeyVault:ClientSecret"]);
})
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
我有以下代码在ConfigureServices内的Startup.cs中播种数据,因此我可以获得Azure KeyVault值:
Task.Run(async () =>
{
try
{
var context = services.BuildServiceProvider().GetRequiredService<ApplicationDbContext>();
var userManager = services.BuildServiceProvider().GetRequiredService<UserManager<ApplicationUser>>();
await SeedData.Initialize(context, userManager, Configuration);
}
catch (Exception ex)
{
var logger = services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
});
方法:
public class SeedData
{
public static async Task Initialize(ApplicationDbContext context, UserManager<ApplicationUser> userManager, IConfiguration configuration)
{
// Look for any users.
if (context.Users.Any())
{
return; // DB has been seeded
}
var user = new ApplicationUser
{
Email = configuration["SeedData:InitialAdminUser"],
UserName = configuration["SeedData:InitialAdminUser"]
};
var password = configuration["SeedData:InitialAdminPassword"];
await userManager.CreateAsync(user, password);
await userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "admin"));
}
}
要播种数据,建议在Program.cs中使用它而不是Startup。那是为什么?
我在Program.cs中有这个,但我无法从AzureKeyVault获取配置值,因为AzureKeyVault是在(我在此阶段无法获得azure密钥库隐私值)之后配置的:< / p>
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
Task.Run(async () =>
{
// seeding initial admin user
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var builder = services.GetRequiredService<IConfigurationRoot>();
await SeedData.Initialize(context, userManager, builder);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
});
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables();
var builtConfig = config.Build();
config.AddAzureKeyVault(
$"https://{builtConfig["KeyVault:Name"]}.vault.azure.net/",
builtConfig["KeyVault:ClientId"],
builtConfig["KeyVault:ClientSecret"]);
})
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
}
要在Program.cs中实现这一点,我认为一种方法是在Task.Run之前将azurekeyvault移动到main?
你在想什么?