Beispiel #1
0
        private void CreateTestUser(int quantity)
        {
            // data test
            var UsersTestToAdd = new List <User>();

            for (var i = 1; i <= quantity; i++)
            {
                var user = new User
                {
                    Name               = $"User {i}",
                    UserName           = $"user{i}",
                    Email              = $"user{i}@itlab.com.br",
                    Active             = (i % 8 != 0),
                    CreationUser       = _context.GetCurrentUser(),
                    AuthenticationType = (i % 5 == 0) ? AuthenticationType.ActiveDirectory : AuthenticationType.DataBase
                };
                UsersTestToAdd.Add(user);
            }
            //

            var userStore   = new UserStore <User, Role, int, UserLogin, UserRole, UserClaim>(_context);
            var userManager = new ApplicationUserManager(userStore)
            {
                UserLockoutEnabledByDefault = true
            };

            var roleStore   = new RoleStore <Role, int, UserRole>(_context);
            var roleManager = new ApplicationRoleManager(roleStore);

            var groupManagerStore = new ApplicationGroupStore(_context);
            var groupManager      = new ApplicationGroupManager(groupManagerStore, roleManager, userManager);

            foreach (var user in UsersTestToAdd)
            {
                //if (userManager.FindByEmail(user.Email) != null) continue;

                //TODO: Devemos usar o CreateAsync, porém, não conseguimos utilizar dentro do foreach, depois precisa verificar o porque.
                var result = userManager.Create(user, "123456");
                if (!result.Succeeded)
                {
                    throw new Exception(string.Join(",", result.Errors.ToArray()));
                }

                groupManager.SetUserGroups(user.Id, GroupsToAdd.Select(x => x.Id).ToArray());

                foreach (var country in CountryAdd)
                {
                    if (user.Countries.All(d => d.Id != country.Id))
                    {
                        user.Countries.Add(country);
                    }
                }
            }
        }
Beispiel #2
0
        private void SetUser()
        {
            var userStore   = new UserStore <User, Role, int, UserLogin, UserRole, UserClaim>(_context);
            var userManager = new ApplicationUserManager(userStore)
            {
                UserLockoutEnabledByDefault = true
            };

            var roleStore   = new RoleStore <Role, int, UserRole>(_context);
            var roleManager = new ApplicationRoleManager(roleStore);

            var groupManagerStore = new ApplicationGroupStore(_context);
            var groupManager      = new ApplicationGroupManager(groupManagerStore, roleManager, userManager);

            foreach (var user in UsersToAdd)
            {
                if (userManager.FindByEmail(user.Email) != null)
                {
                    continue;
                }

                var result = userManager.Create(user, "123456");
                if (!result.Succeeded)
                {
                    throw new Exception(string.Join(",", result.Errors.ToArray()));
                }

                groupManager.SetUserGroups(user.Id, GroupsToAdd.Select(x => x.Id).ToArray());

                foreach (var country in CountryAdd)
                {
                    if (user.Countries.All(d => d.Id != country.Id))
                    {
                        user.Countries.Add(country);
                    }
                }
            }
        }