我开始使用Identity进行身份验证的新ASP.NET Core MVC项目。 我想添加一个默认的超级用户到asp数据库,所以它可以添加新用户,但我不知道该怎么做。使用默认超级用户种子ASP.NET Core 1.1数据库
首先,我不知道这是否是使用相同的数据库用户认证/授权和应用程序的其余部分是一个好主意,或者我应该使用不同的数据库。
其次,我需要知道如何播种“ASP数据库”使用默认的超级用户。
在从StackOverflow的this解决方案,我知道如何访问数据库,但我想也abble得到“的UserManager”实例使用管理超级用户添加到数据库中发生的背景的。
我在启动类的代码:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Seed(app);
}
public void Seed(IApplicationBuilder app)
{
using (var context = app.ApplicationServices.GetRequiredService<ApplicationDbContext>())
{
//... perform other seed operations
}
}