protected override void Up(MigrationBuilder migrationBuilder) { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var dbContext = new SampleDataBaseContext(new DbContextOptions <SampleDataBaseContext>()); var userStore = new CustomUserStore(dbContext); var userManager = new UserManager <User>(userStore, null, null, null, null, null, null, null, null); var user = new User { UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", PhoneNumber = "+111111111111", EmailConfirmed = true, PhoneNumberConfirmed = true, SecurityStamp = Guid.NewGuid().ToString("D"), LockoutEnabled = true, }; string password = "******"; //var result = userManager.CreateAsync(user, password).Result; var hashed = new PasswordHasher <User>().HashPassword(user, password); user.PasswordHash = hashed; var result = userStore.CreateAsync(user).Result; userStore.AddToRoleAsync(user, RoleEnum.Admin.ToString()).Wait(); dbContext.SaveChanges(); scope.Complete(); } }
protected override void Up(MigrationBuilder migrationBuilder) { using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { var dbContext = new SampleDataBaseContext(new DbContextOptions <SampleDataBaseContext>()); var userStore = new CustomUserStore(dbContext); var userManager = new UserManager <User>(userStore, null, null, null, null, null, null, null, null); var user = new User { UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", PhoneNumber = null, EmailConfirmed = true, PhoneNumberConfirmed = false, SecurityStamp = Guid.NewGuid().ToString("D"), LockoutEnabled = true, }; string password = "******"; var hashed = new PasswordHasher <User>().HashPassword(user, password); user.PasswordHash = hashed; var result = userStore.CreateAsync(user).Result; userStore.AddToRoleAsync(user, RoleEnum.Member.ToString()).Wait(); var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.UserName), new Claim(ClaimTypes.System, "crud"), }; userManager.AddClaimsAsync(user, claims).Wait(); dbContext.SaveChanges(); scope.Complete(); } }
private async Task SetUpTests() { userList = new List <User> { new User { Id = Guid.NewGuid(), FirstName = "Super", LastName = "Man", EmailAddress = "*****@*****.**", UserName = "******" }, new User { Id = Guid.NewGuid(), FirstName = "Spider", LastName = "Dude", EmailAddress = "*****@*****.**", UserName = "******" }, new User { Id = Guid.NewGuid(), FirstName = "Green", LastName = "Lantern", EmailAddress = "*****@*****.**", UserName = "******" }, }; foreach (var user in userList) { await userStore.CreateAsync(user); } var adminUser = await userStore.FindByNameAsync(userList[0].EmailAddress); businessList = new List <Business> { new Business { Name = "Random busines busines", Description = "Description of business", Email = "*****@*****.**", Phone = "1234567", Users = new List <User> { adminUser } }, new Business { Name = "Second busines", Description = "Description of business", Email = "*****@*****.**", Phone = "123456", Users = new List <User> { adminUser } }, new Business { Name = "Third busines", Description = "Description of business", Email = "*****@*****.**", Phone = "123456", Users = new List <User> { adminUser } } }; businessList.ForEach(bus => _orgRepo.Insert(bus)); }
public static async Task SeedUser() { var user = new ApplicationUser { UserName = "******", NormalizedUserName = "******", Email = "*****@*****.**", NormalizedEmail = "*****@*****.**", EmailConfirmed = true, LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; var roleStore = new CustomRoleStore(_context); IList <ApplicationRole> roles = new List <ApplicationRole>() { new ApplicationRole() { Name = MembershipConstant.SuperAdmin, NormalizedName = MembershipConstant.SuperAdmin.ToUpper() }, new ApplicationRole() { Name = MembershipConstant.Admin, NormalizedName = MembershipConstant.Admin.ToUpper() }, new ApplicationRole() { Name = MembershipConstant.Operator, NormalizedName = MembershipConstant.Operator.ToUpper() }, new ApplicationRole() { Name = MembershipConstant.Staff, NormalizedName = MembershipConstant.Staff.ToUpper() }, new ApplicationRole() { Name = MembershipConstant.Owner, NormalizedName = MembershipConstant.Owner.ToUpper() }, new ApplicationRole() { Name = MembershipConstant.Member, NormalizedName = MembershipConstant.Member.ToUpper() } }; foreach (var role in roles) { //var existRole = await roleStore.FindByNameAsync(role.NormalizedName); if (!_context.Roles.Any(x => x.Name == role.Name)) { await roleStore.CreateAsync(new ApplicationRole { Name = role.Name, NormalizedName = role.NormalizedName }); } } if (!_context.Users.Any(u => u.UserName == user.UserName)) { var password = new PasswordHasher <ApplicationUser>(); var hashed = password.HashPassword(user, "test123!"); user.PasswordHash = hashed; var userStore = new CustomUserStore(_context); await userStore.CreateAsync(user); await userStore.AddToRoleAsync(user, MembershipConstant.SuperAdmin); } }