public static void InitializeDevelopmentEnvironment(IdentityDatabaseContext context)
        {
            context.Database.Migrate();
            if (context.Roles.Any())
            {
                return; //DB has been seeded
            }

            SeedRolesToDatabase(context);
            context.SaveChanges(); //Double guard in case you need more seeding, async changes etc
        }
 private static void SeedRolesToDatabase(IdentityDatabaseContext context)
 {
     //TODO: Figure out some sort of for roles in role in order to avoid having to remember all roles and remember to add them. This is just another step of misdirection in which we can forget to add all the roles
     context.Roles.Add(new IdentityRole(IdentityDatabaseContext.AdministratorRoleString)
     {
         NormalizedName = IdentityDatabaseContext.AdministratorRoleString.Normalize()
     });
     context.Roles.Add(new IdentityRole(IdentityDatabaseContext.PrivilegedShitlordRoleString)
     {
         NormalizedName = IdentityDatabaseContext.PrivilegedShitlordRoleString.Normalize()
     });
     context.Roles.Add(new IdentityRole(IdentityDatabaseContext.BaseUsersRoleString)
     {
         NormalizedName = IdentityDatabaseContext.BaseUsersRoleString.Normalize()
     });
     context.SaveChanges();
 }
예제 #3
0
        private static ApplicationUser CreateUser(IdentityDatabaseContext context, string login, string password,
                                                  string eMail, string role)
        {
            var user = new ApplicationUser
            {
                UserName           = login,
                Email              = eMail,
                NormalizedUserName = login.Normalize(),
                NormalizedEmail    = eMail.Normalize(),
                EmailConfirmed     = false,
                LockoutEnabled     = false,
                SecurityStamp      = Guid.NewGuid().ToString(),
            };
            var passwordHasher = new PasswordHasher <ApplicationUser>();

            user.PasswordHash = passwordHasher.HashPassword(user, password);
            var userStore = new UserStore <ApplicationUser>(context);

            userStore.CreateAsync(user).Wait();
            userStore.AddToRoleAsync(user, role).Wait();

            context.SaveChanges();
            return(user);
        }