c# - 我无法在种子数据中创建管理员
问题描述
到目前为止我尝试了什么
public class SeedIdentity
{
public static async Task Seed(IApplicationBuilder app)
{
var identityContext = app.ApplicationServices.GetRequiredService<ApplicationIdentityDbContext>();
identityContext.Database.Migrate();
if (!identityContext.Roles.Any(i => i.Name == "Admin" && i.Name == "User"))
{
var roleStore = new RoleStore<IdentityRole>(identityContext);
var roleManager = new RoleManager<IdentityRole>(roleStore, null, null, null, null);
var adminRole = new IdentityRole { Name = "admin" };
var userRole = new IdentityRole { Name = "user" };
await roleManager.CreateAsync(adminRole);
await roleManager.CreateAsync(userRole);
identityContext.SaveChanges();
}
if (!identityContext.Users.Any(i => i.UserName == "akinaldemir"))
{
var userStore = new UserStore<ApplicationUser>(identityContext);
var userManager = new UserManager<ApplicationUser>(userStore, null, null, null,null, null, null, null, null);
var admin = new ApplicationUser()
{
UserName = "akinaldemir",
Email = "akinaldemir@gmail.com"
};
await userManager.CreateAsync(admin, "Abc12!jtre");
await userManager.AddToRoleAsync(admin, "admin");
identityContext.SaveChanges();
}
}
}
解决方案
推荐阅读
- python - PyInstaller 不显示打印语句
- ionic-framework - 如何使用电容器将 UrbanAirShip 推送通知插件集成到 ionic 5 角度?
- java - 通过 JSON-B + Jax-RS 使用 Java 记录
- java - Spring boot 不渲染 JSP 页面,Spring boot 新版本(2.5.0)
- c# - 我的 ASP.NET Core MVC 应用程序可以在本地请求并从外部 api 获取答案,但在 win 服务器上不能
- json - JAXB Unmarshalling json with unknown field with special character throwing DOM exception invalid character error while trying to map XMLAnyElement
- javascript - 如何获得客户端上传速度
- android - 在应用程序中,我不断收到 UNRESOLVED REFERENCE: ANDROID 错误
- php - 方法 Illuminate\Http\UploadedFile::validate 不存在
- rust - 如何获得最小 BST 元素?