protected override void Seed(B1625DbContext context) { base.Seed(context); UserAccountManager accountManager = UserAccountManager.Create(context); UserRoleManager roleManager = new UserRoleManager(new RoleStore <UserRole>(context)); roleManager.Create(new UserRole() { Name = "Administrators" }); roleManager.Create(new UserRole() { Name = "Moderators" }); roleManager.Create(new UserRole() { Name = "Users" }); var admin = accountManager.FindByName("admin"); if (admin == null) { admin = new UserAccount() { UserName = "******", Email = "*****@*****.**" }; var profile = new UserProfile() { AccountId = admin.Id, Gender = Gender.Male, RegistrationDate = DateTime.Now }; admin.Profile = profile; var result = accountManager.Create(admin, "adminadmin"); if (!result.Succeeded) { } accountManager.AddToRole(admin.Id, "Administrators"); accountManager.AddToRole(admin.Id, "Users"); context.SaveChanges(); } }
public async Task <IActionResult> Create([Bind("Id,UserId,RoleId")] UserRole userRole) { if (ModelState.IsValid) { await UserRoleManager.Create(userRole); return(RedirectToAction(nameof(Index))); } //ViewData["Roles"] = _context.Roles; //ViewData["Users"] = _context.UserBases; // 生成选择框数据 ViewData["RoleId"] = new SelectList(RoleManager.Find(), nameof(Role.Id), nameof(Role.Name), userRole.RoleId); ViewData["UserId"] = new SelectList(UserManager.Find(), nameof(Entitys.User.Id), nameof(Entitys.User.SignName), userRole.UserId); return(View(userRole)); }
public void AddUserRole(AspNetUserRole AspNetUserRole) { aspNetUserRolesManager.Create(AspNetUserRole); aspNetUserRolesManager.Save(); }