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 ActionResult AddNewEmployee(NewEmployeeViewModel model) { if (!ModelState.IsValid) { model.TrainsIds = GetTrainsIds(); return(View(model)); } var user = new ApplicationUser() { UserName = model.Login }; var r = _userManager.Create(user, model.Password); if (!r.Succeeded) { int t = 0; foreach (var e in r.Errors) { ModelState.AddModelError("auth" + t++, e); } model.TrainsIds = GetTrainsIds(); return(View(model)); } Employee employee = new Employee() { Name = model.Name, Age = model.Age, Phone = model.Phone, TrainId = model.TrainId, UserId = user.Id, Position = model.Position }; try { _context.Employees.Add(employee); _context.SaveChanges(); } catch (Exception e) { ModelState.AddModelError("employee", e); model.TrainsIds = GetTrainsIds(); return(View(model)); } return(RedirectToAction("Index")); }
public ActionResult Register(RegisterViewModel rvm) { var au = new ApplicationUser() { UserName = rvm.Login }; var r = _userManager.Create(au, rvm.Password); if (!r.Succeeded) { int t = 0; foreach (var e in r.Errors) { ModelState.AddModelError("auth" + t++, e); } return(View("Register", rvm)); } return(RedirectToAction("Login")); }