How do I seed a "admin" user role when identity is scaffolded?
I would like to find my power-user account by email and seed the admin role.
Most examples I find use Startup.cs
, but you are supposed to use IdentityHostingStartup.cs
to register identity related services.
So where/how do I inject the RoleManager
and UserManager
in IdentityHostingStartup
? (I assume this is what I need, please let me know if there is a better way)
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;
});
});
}
}