Example #1
0
        public static bool UpdateUser(User user, OU targetOU, string firstname, string lastname, string loginName, string password, bool isEnabled, DateTime?accountExpirationDate)
        {
            user.UserPrincipal.GivenName         = firstname;
            user.UserPrincipal.Surname           = lastname;
            user.UserPrincipal.DisplayName       = firstname + " " + lastname;
            user.UserPrincipal.SamAccountName    = loginName;
            user.UserPrincipal.UserPrincipalName = loginName + AD.ADDomainEmail;

            if (password.Trim() != "")
            {
                user.UserPrincipal.SetPassword(password);
            }
            user.UserPrincipal.Enabled = isEnabled;
            user.UserPrincipal.AccountExpirationDate = accountExpirationDate;
            try
            {
                user.UserPrincipal.Save();
                user.SamAccountName = loginName;
            }
            catch (Exception fout)
            {
                return(false);
            }
            if (targetOU.Path != user.DirectoryEntry.Path)
            {
                OUService.MovePrincipal(user, targetOU);
            }
            return(true);
        }
        public static Group UpdateGroup(Group group, OU targetOU, string groupName)
        {
            Group retourGroup = null;

            try
            {
                // dit kan wel ???
                group.GroupPrincipal.SamAccountName = groupName;
                group.GroupPrincipal.Save();

                // Name prop is readonly bij een bestaande groep, dus onderstaande werkt niet ?????
                // group.GroupPrincipal.Name = groupName;
                //
                // Wat dan wel werkt : (hierdoor wordt op AD blijkbaar wel een nieuwe group-object gemaakt, dus ik vermoed wissen en nieuw maken):
                // ===========================================================
                DirectoryEntry directoryEntry = new DirectoryEntry(AD.LDAPShort + group.GroupPrincipal.DistinguishedName);
                directoryEntry.Rename("CN=" + groupName);
                // ===========================================================

                retourGroup = new Group(groupName);
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
            if (targetOU.Path != retourGroup.DirectoryEntry.Path)
            {
                OUService.MovePrincipal(retourGroup, targetOU);
            }
            return(retourGroup);
        }
Example #3
0
        public static User CreateUser(OU targetOU, string firstname, string lastname, string loginName, string password, bool isEnabled, DateTime?accountExpirationDate)
        {
            // onderstaande zou moeten werken (= gebruiker meteen in correcte OU plaatsen) maar werkt niet
            //PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, AD.ADDomainNameShort, targetOU.Path);
            // dan maar nieuwe gebruiker in de OU in de "CN=Users,DC=ait,DC=local" plaatsen en achteraf verplaatsen naar targetOU
            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
            UserPrincipal    userPrincipal    = new UserPrincipal(principalContext);

            userPrincipal.GivenName         = firstname;
            userPrincipal.Surname           = lastname;
            userPrincipal.DisplayName       = firstname + " " + lastname;
            userPrincipal.SamAccountName    = loginName;
            userPrincipal.UserPrincipalName = loginName + AD.ADDomainEmail;
            userPrincipal.SetPassword(password);
            userPrincipal.Enabled = isEnabled;
            userPrincipal.AccountExpirationDate = accountExpirationDate;
            try
            {
                userPrincipal.Save();
                User user = new User(userPrincipal.SamAccountName);
                OUService.MovePrincipal(user, targetOU);
                return(user);
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }
        public static Group CreateGroup(OU targetOU, string groupName)
        {
            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
            GroupPrincipal   groupPrincipal   = new GroupPrincipal(principalContext);

            groupPrincipal.Name           = groupName;
            groupPrincipal.SamAccountName = groupName;
            try
            {
                groupPrincipal.Save();
                Group group = new Group(groupPrincipal.SamAccountName);
                OUService.MovePrincipal(group, targetOU);
                return(group);
            }
            catch (Exception error)
            {
                throw new Exception(error.Message);
            }
        }