【发布时间】:2018-10-08 18:52:40
【问题描述】:
当身份被搭建时,我如何为“管理员”用户角色播种?
我想通过电子邮件查找我的超级用户帐户并设置管理员角色。
我发现大多数示例使用Startup.cs,但您应该使用IdentityHostingStartup.cs 注册身份相关服务。
那么我在哪里/如何在IdentityHostingStartup 中注入RoleManager 和UserManager? (我认为这是我需要的,如果有更好的方法请告诉我)
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
services.AddDbContext<MyWebContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("MyWebContextConnection")));
services.AddIdentity<MyWebUser, MyWebRole>()
.AddRoles<MyWebRole>()
.AddRoleManager<RoleManager<MyWebRole>>()
.AddDefaultUI()
.AddEntityFrameworkStores<MyWebContext>();
services.Configure<IdentityOptions>(options => {
options.Password.RequireNonAlphanumeric = false;
});
});
}
}
【问题讨论】:
标签: c# asp.net-core asp.net-identity