protected override void Seed(Nsu.Belov.TrainsDatabase.Database.TrainsDataContext context) { UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context); RoleStore<IdentityRole> roleStore = new RoleStore<IdentityRole>(context); TrainsUserManager userManager = new TrainsUserManager(userStore); TrainsRoleManager roleManager = new TrainsRoleManager(roleStore); IdentityRole adminRole = context.Roles.FirstOrDefault(x => x.Name == "admin"); if (adminRole == null) { roleManager.Create(new IdentityRole("admin")); } IdentityRole operatorRole = context.Roles.FirstOrDefault(x => x.Name == "operator"); if (operatorRole == null) { roleManager.Create(new IdentityRole("operator")); } ApplicationUser admin = context.Users.FirstOrDefault(u => u.UserName == "admin"); if (admin == null) { admin = new ApplicationUser() { UserName = "******" }; userManager.Create(admin, "admin1"); //незнаю можно ли не сохраная контекст достать еще раз через него admin = userManager.FindByName("admin"); } userManager.AddToRole(admin.Id, "admin"); userManager.AddToRole(admin.Id, "operator"); }
public TableAdjustment AddRole(LatticeData <Employee, EmployeeRow> latticeData) { var comandModel = latticeData.CommandConfirmation <TargetRoleCommandViewModel>(); var subj = latticeData.CommandSubject(); var employee = _context.Employees.FirstOrDefault(x => x.UserId == subj.UserId); _userManager.AddToRole(employee.UserId, comandModel.TargetRole); return(latticeData.Adjust(x => x .Message(LatticeMessage.User("success", "Remove", "Role was added")) )); }