Пример #1
0
        public async Task Initialize(ApplicationDBcontext context)
        {
            context.Database.EnsureCreated();

            if (context.Users.Any())
            {
                return; // Db has been seeded.
            }

            await rolemanager.CreateAsync(new IdentityRole("administrator"));

            await rolemanager.CreateAsync(new IdentityRole("user"));


            var user = new ApplicationUser
            {
                givenname          = "mo",
                familyname         = "bouzim",
                AccessFailedCount  = 0,
                Email              = "*****@*****.**",
                EmailConfirmed     = false,
                LockoutEnabled     = true,
                NormalizedEmail    = "*****@*****.**",
                NormalizedUserName = "******",
                TwoFactorEnabled   = false,
                UserName           = "******"
            };

            var result = await usermanager.CreateAsync(user, "Admin01*");

            var roleresult = await usermanager.AddToRoleAsync(user, "administrator");



            Random         r = new Random();
            int            tempusercount = 0;
            IdentityResult resultusers, roleresultusers;

            do
            {
                var usercreated = new ApplicationUser
                {
                    givenname          = GenerateName(r.Next(4, 10), r),
                    familyname         = GenerateName(r.Next(4, 10), r),
                    AccessFailedCount  = 0,
                    Email              = "*****@*****.**",
                    EmailConfirmed     = false,
                    LockoutEnabled     = true,
                    NormalizedEmail    = "*****@*****.**",
                    NormalizedUserName = "******",
                    TwoFactorEnabled   = false,
                    UserName           = GenerateName(r.Next(6, 11), r)
                };

                resultusers = await usermanager.CreateAsync(usercreated, "Admin01*");

                roleresultusers = await usermanager.AddToRoleAsync(usercreated, "user");

                tempusercount++;
            } while (resultusers.Succeeded && roleresultusers.Succeeded && tempusercount < COUNT_USERS);
        }
Пример #2
0
        public async Task Initialize(ApplicationDBcontext context)
        {
            context.Database.EnsureCreated();

            if (context.Users.Any())
            {
                return; // Db has been seeded.
            }

            await rolemanager.CreateAsync(new IdentityRole("administrator"));

            await rolemanager.CreateAsync(new IdentityRole("user"));


            var user = new ApplicationUser
            {
                voornaam           = "mo",
                achternaam         = "bouzim",
                lievelingskleur    = "groen",
                AccessFailedCount  = 0,
                Email              = "*****@*****.**",
                EmailConfirmed     = false,
                LockoutEnabled     = true,
                NormalizedEmail    = "*****@*****.**",
                NormalizedUserName = "******",
                TwoFactorEnabled   = false,
                UserName           = "******"
            };

            var result = await usermanager.CreateAsync(user, "Admin01*");

            if (result.Succeeded)
            {
                var adminUser = await usermanager.FindByNameAsync(user.UserName);

                // Assigns the administrator role.
                await usermanager.AddToRoleAsync(adminUser, "administrator");

                // Assigns claims.
                var claims = new List <Claim> {
                    new Claim(type: JwtClaimTypes.GivenName, value: user.voornaam),
                    new Claim(type: JwtClaimTypes.FamilyName, value: user.achternaam),
                };
                await usermanager.AddClaimsAsync(adminUser, claims);
            }
            else
            {
                Debug.WriteLine(result.ToString());
            }

            user = new ApplicationUser
            {
                voornaam           = "mo",
                achternaam         = "bouzim",
                lievelingskleur    = "groen",
                AccessFailedCount  = 0,
                Email              = "*****@*****.**",
                EmailConfirmed     = false,
                LockoutEnabled     = true,
                NormalizedEmail    = "*****@*****.**",
                NormalizedUserName = "******",
                TwoFactorEnabled   = false,
                UserName           = "******"
            };

            result = await usermanager.CreateAsync(user, "Mo12345678*");

            if (result.Succeeded)
            {
                var adminUser = await usermanager.FindByNameAsync(user.UserName);

                // Assigns the administrator role.
                await usermanager.AddToRoleAsync(adminUser, "user");

                // Assigns claims.
                var claims = new List <Claim> {
                    new Claim(type: JwtClaimTypes.GivenName, value: user.voornaam),
                    new Claim(type: JwtClaimTypes.FamilyName, value: user.achternaam),
                };
                await usermanager.AddClaimsAsync(adminUser, claims);
            }
            else
            {
                Debug.WriteLine(result.ToString());
            }
        }