Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="grp"></param>
        /// <returns></returns>
        static public List <User> extractUsers(int start, int end, List <string> members)
        {
            Console.Write("\n");
            List <User> users = new List <User>();
            Color       myC   = start > 0 ? Color.Cyan : Color.Yellow;

            UserPrincipalExt extUser;

            for (int x = start; x < end; x++)
            {
                if (start > 0)
                {
                    Console.Write("\r User " + x + "     ");
                }
                try {
                    extUser = UserPrincipalExt.FindByIdentity(principalContext, IdentityType.SamAccountName, members[x]);
                    users.Add(new User(extUser));
                }
                catch (Exception ee) {
                    Console.WriteFormatted("\n\t| Exception user" + x + "(" + members[x] + "): " + ee.Message + "\n", Color.Red);
                }
            }
            return(users);
        }
Пример #2
0
        public User Create(string usersOU, string clearTextPassword, User userObject)
        {
            PrincipalContext ctx = null;
            UserPrincipalExt usr = null;

            try
            {
                log.Debug("Attempting to create new user");

                if (string.IsNullOrEmpty(usersOU))
                {
                    throw new MissingFieldException("User", "usersOU");
                }

                if (string.IsNullOrEmpty(clearTextPassword))
                {
                    throw new MissingFieldException("User", "clearTextPassword");
                }

                if (string.IsNullOrEmpty(userObject.sAMAccountName))
                {
                    throw new MissingFieldException("User", "SamAccountName");
                }

                if (string.IsNullOrEmpty(userObject.UserPrincipalName))
                {
                    throw new MissingFieldException("User", "UserPrincipalName");
                }

                if (string.IsNullOrEmpty(userObject.Firstname))
                {
                    throw new MissingFieldException("User", "FirstName");
                }

                if (string.IsNullOrEmpty(userObject.DisplayName))
                {
                    throw new MissingFieldException("User", "DisplayName");
                }

                if (string.IsNullOrEmpty(userObject.Name))
                {
                    throw new MissingFieldException("User", "Name");
                }

                // Check if the user exists
                pc  = GetPrincipalContext(); // Used for querying purposes
                usr = UserPrincipalExt.FindByIdentity(pc, IdentityType.UserPrincipalName, userObject.UserPrincipalName);
                if (usr != null)
                {
                    throw new PrincipalExistsException(userObject.UserPrincipalName);
                }

                // Now we can create the user!
                userObject.sAMAccountName = GetAvailableSamAccountName(userObject.UserPrincipalName);
                ctx = new PrincipalContext(ContextType.Domain, this._domainController, usersOU, this._username, this._password); // Used for creating new user
                usr = new UserPrincipalExt(ctx, userObject.sAMAccountName, clearTextPassword, true);
                usr.UserPrincipalName = userObject.UserPrincipalName;
                usr.DisplayName       = userObject.DisplayName;
                usr.Name      = userObject.Name;
                usr.GivenName = userObject.Firstname;

                if (!string.IsNullOrEmpty(userObject.Lastname))
                {
                    usr.LastName = userObject.Lastname;
                }

                if (!string.IsNullOrEmpty(userObject.Department))
                {
                    usr.Department = userObject.Department;
                }

                usr.Save();

                // After we save we need to return some data
                userObject.UserGuid          = (Guid)usr.Guid;
                userObject.DistinguishedName = usr.DistinguishedName;

                return(userObject);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error retrieving user {0}. Exception: {1}", userObject.UserPrincipalName, ex.ToString());
                throw;
            }
            finally
            {
                if (usr != null)
                {
                    usr.Dispose();
                }
            }
        }