【发布时间】:2018-02-09 15:51:08
【问题描述】:
我正在使用 Identity 进行身份验证开始一个新的 ASP.NET Core MVC 项目。 我想在asp数据库中添加一个默认的超级用户,这样它就可以添加新用户了,但我不知道该怎么做。
首先,我不知道为用户的身份验证/授权和应用程序的其余部分使用相同的数据库是否是个好主意,或者我是否应该使用不同的数据库。
其次,我需要知道如何使用默认超级用户播种“asp 数据库”。
遵循 StackOverflow 的 this 解决方案,我知道如何访问数据库,但我还想获得一个“userManager”实例,以使用管理器代替上下文。
我在 Startup 课上有这段代码:
// 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
}
}
【问题讨论】:
标签: c# entity-framework-core asp.net-core-1.1 asp.net-core-identity