【发布时间】:2018-02-28 20:53:11
【问题描述】:
public class DbInitializer
{
public static async Task CreateAdmin(IServiceProvider service)
{
UserManager<AppUser> userManager = service.GetRequiredService<UserManager<AppUser>>();
RoleManager<IdentityRole> roleManager = service.GetRequiredService<RoleManager<IdentityRole>>();
string username = "Admin";
string email = "AdminG@example.com";
string pass = "Secrete90";
string role = "Admins";
if(await userManager.FindByNameAsync(username)== null)
{
if(await roleManager.FindByNameAsync(role)== null)
{
await roleManager.CreateAsync(new IdentityRole(role));
}
var user = new AppUser { UserName = username, Email = email };
var result = await userManager.CreateAsync(user, pass);
if (result.Succeeded) { await userManager.AddToRoleAsync(user, role); }
}
}
当我在启动时运行此代码时,我收到一个关于无法在启动类中限定代码范围的错误。
DbInitializer.CreateAdmin(app.ApplicationServices).Wait();
【问题讨论】:
标签: c# asp.net-core asp.net-core-2.0 asp.net-core-identity