public static void SeedUserRoles(this DutContext context, List <string> Roles) { var roleManager = new RoleManager <DutRole>(new RoleStore <DutRole>(context)); foreach (var role in Roles) { var roleexists = roleManager.RoleExists(role); if (roleexists) { continue; } roleManager.Create(new DutRole { Name = role }); } }
public static void SeedAdminAccount(this DutContext context, string userName, string passaword) { var userManager = new UserManager <DutUser>(new UserStore <DutUser>(context)); var user = userManager.Find(userName, passaword); if (user != null) { return; } SeedUserRoles(context, new List <string> { "admin", "user" }); user = new DutUser { UserName = userName }; var result = userManager.Create(user, passaword); if (result.Succeeded) { var factory = new DataBaseFactory(); factory.Get(); userManager.AddToRole(user.Id, "admin"); var m = new UserProfileRepository(factory); m.Add(new ClientProfile { Id = user.Id, Enable = true, CreateDate = DateTime.Now }); factory.Get().SaveChanges(); } else { var e = new Exception("Could not add default account"); var enumerator = result.Errors.GetEnumerator(); foreach (var resultError in result.Errors) { e.Data.Add(enumerator.Current, resultError); } throw e; } }