Example #1
0
        private void GetADUsers()
        {
            _adUsers = new List <ADUserModel>();

            using (var context = new PrincipalContext(ContextType.Domain, _domainName))
            {
                using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
                {
                    foreach (var result in searcher.FindAll())
                    {
                        DirectoryEntry entry = result.GetUnderlyingObject() as DirectoryEntry;
                        if (entry.Properties["mail"].Value != null && entry.Properties["givenName"].Value != null && entry.Properties["sn"].Value != null)
                        {
                            ADUserModel user = new ADUserModel()
                            {
                                DomainName = _domainName,
                                Email      = GetString(entry.Properties["mail"].Value),
                                FirstName  = GetString(entry.Properties["givenName"].Value),
                                LastName   = GetString(entry.Properties["sn"].Value),
                                UserName   = GetString(entry.Properties["samAccountName"].Value)
                            };

                            _adUsers.Add(user);
                        }
                    }
                }
            }
        }
Example #2
0
 public MSActorReturnMessageModel NewADUser([FromBody] ADUserModel newUser)
 {
     try {
         ADController control = new ADController();
         return(control.NewADUserDriver(newUser));
     }catch (Exception e)
     {
         return(util.ReportError(e));
     }
 }
Example #3
0
        public ADUserModel GetADUserDriver(string emplid)
        {
            string searchName                      = "";
            string searchCity                      = "";
            string searchCountry                   = "";
            string searchDepartment                = "";
            string searchDescription               = "";
            string searchDisplayName               = "";
            string searchEmployeeID                = "";
            string searchGivenName                 = "";
            string searchOfficePhone               = "";
            string searchInitials                  = "";
            string searchOffice                    = "";
            string searchSamAccountName            = "";
            string searchState                     = "";
            string searchStreetAddress             = "";
            string searchSurname                   = "";
            string searchTitle                     = "";
            string searchObjectClass               = "";
            string searchUserPrincipalName         = "";
            string searchPath                      = "";
            string searchPostalCode                = "";
            string searchType                      = "";
            string searchIPPhone                   = "";
            string searchMSExchHideFromAddressList = "";
            string searchChangePasswordAtLogon     = "";

            string searchEnabled = "";

            try
            {
                using (PowerShell ps = PowerShell.Create())
                {
                    ps.AddCommand("get-aduser");
                    ps.AddParameter("Filter", "Name -eq " + emplid);
                    ps.AddParameter("Properties", "*");
                    Collection <PSObject> names = ps.Invoke();
                    PSObject ob = names.FirstOrDefault();
                    if (ob != null)
                    {
                        if (ob.Properties["samaccountname"].Value != null)
                        {
                            searchName = ob.Properties["samaccountname"].Value.ToString();
                        }
                        if (ob.Properties["City"].Value != null)
                        {
                            searchCity = ob.Properties["City"].Value.ToString();
                        }
                        if (ob.Properties["Country"].Value != null)
                        {
                            searchCountry = ob.Properties["Country"].Value.ToString();
                        }
                        if (ob.Properties["Department"].Value != null)
                        {
                            searchDepartment = ob.Properties["Department"].Value.ToString();
                        }
                        if (ob.Properties["Description"].Value != null)
                        {
                            searchDescription = ob.Properties["Description"].Value.ToString();
                        }
                        if (ob.Properties["DisplayName"].Value != null)
                        {
                            searchDisplayName = ob.Properties["DisplayName"].Value.ToString();
                        }
                        if (ob.Properties["EmployeeID"].Value != null)
                        {
                            searchEmployeeID = ob.Properties["EmployeeID"].Value.ToString();
                        }
                        if (ob.Properties["GivenName"].Value != null)
                        {
                            searchGivenName = ob.Properties["GivenName"].Value.ToString();
                        }
                        if (ob.Properties["OfficePhone"].Value != null)
                        {
                            searchOfficePhone = ob.Properties["OfficePhone"].Value.ToString();
                        }
                        if (ob.Properties["Initials"].Value != null)
                        {
                            searchInitials = ob.Properties["Initials"].Value.ToString();
                        }
                        if (ob.Properties["Office"].Value != null)
                        {
                            searchOffice = ob.Properties["Office"].Value.ToString();
                        }
                        if (ob.Properties["SamAccountName"].Value != null)
                        {
                            searchSamAccountName = ob.Properties["SamAccountName"].Value.ToString();
                        }
                        if (ob.Properties["State"].Value != null)
                        {
                            searchState = ob.Properties["State"].Value.ToString();
                        }
                        if (ob.Properties["StreetAddress"].Value != null)
                        {
                            searchStreetAddress = ob.Properties["StreetAddress"].Value.ToString();
                        }
                        if (ob.Properties["Surname"].Value != null)
                        {
                            searchSurname = ob.Properties["Surname"].Value.ToString();
                        }
                        if (ob.Properties["Title"].Value != null)
                        {
                            searchTitle = ob.Properties["Title"].Value.ToString();
                        }
                        if (ob.Properties["ObjectClass"].Value != null)
                        {
                            searchObjectClass = ob.Properties["ObjectClass"].Value.ToString();
                        }
                        if (ob.Properties["UserPrincipalName"].Value != null)
                        {
                            searchUserPrincipalName = ob.Properties["UserPrincipalName"].Value.ToString();
                        }
                        if (ob.Properties["Path"].Value != null)
                        {
                            searchPath = ob.Properties["Path"].Value.ToString();
                        }
                        if (ob.Properties["PostalCode"].Value != null)
                        {
                            searchPostalCode = ob.Properties["PostalCode"].Value.ToString();
                        }

                        if (ob.Properties["enabled"].Value != null)
                        {
                            searchEnabled = ob.Properties["enabled"].Value.ToString();
                        }
                        //The following lines contain a field that has not yet been implemented

                        /*if (ob.Properties["ipphone"].Value != null)
                         *  searchIPPhone = ob.Properties["ipphone"].Value.ToString();*/

                        ADUserModel toReturn = new ADUserModel(searchCity, searchName, searchDepartment,
                                                               searchDescription, searchDisplayName, searchEmployeeID, searchGivenName, searchOfficePhone,
                                                               searchInitials, searchOffice, searchPostalCode, searchSamAccountName, searchState,
                                                               searchStreetAddress, searchSurname, searchTitle, searchUserPrincipalName, searchPath, searchIPPhone,
                                                               searchMSExchHideFromAddressList, searchChangePasswordAtLogon, searchEnabled, searchType, "");
                        return(toReturn);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Example #4
0
        /// <summary>
        /// This is a driver method to be called from the MSActorController. it creates a new user in AD, and returns
        /// the status message of the request.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public MSActorReturnMessageModel NewADUserDriver(ADUserModel user)
        {
            // Project P0975: Update retry delays from 1 second to 3 seconds, attempting to
            // reduce error reports from delays in creating user accounts
            try
            {
                using (PowerShell powershell = PowerShell.Create())
                {
                    //Password nonsense to follow
                    PSCommand command = new PSCommand();
                    command.AddCommand("ConvertTo-SecureString");
                    command.AddParameter("AsPlainText");
                    command.AddParameter("String", user.accountPassword);
                    command.AddParameter("Force");
                    powershell.Commands = command;
                    Collection <PSObject> passHashCollection = powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();
                    PSObject toPass = passHashCollection.First();   //this is the password wrapped in a psobject

                    command = new PSCommand();
                    command.AddCommand("new-aduser");
                    command.AddParameter("name", user.name); //Name used to be emplid, but has since been changed
                    command.AddParameter("accountpassword", toPass);
                    command.AddParameter("changepasswordatlogon", user.changepasswordatlogon);
                    command.AddParameter("city", user.city);
                    //command.AddParameter("country", user.country);
                    command.AddParameter("department", user.department);
                    command.AddParameter("description", user.description);
                    command.AddParameter("displayname", user.displayname);
                    command.AddParameter("employeeid", user.employeeid);
                    command.AddParameter("enabled", user.enabled);
                    command.AddParameter("givenname", user.givenname);
                    command.AddParameter("officephone", user.officephone);
                    command.AddParameter("initials", user.initials);
                    command.AddParameter("office", user.office);
                    command.AddParameter("postalcode", user.postalcode);
                    command.AddParameter("samaccountname", user.samaccountname);
                    command.AddParameter("state", user.state);
                    command.AddParameter("streetaddress", user.streetaddress);
                    command.AddParameter("surname", user.surname);
                    command.AddParameter("Title", user.title);
                    command.AddParameter("type", user.type);
                    command.AddParameter("userprincipalname", user.userprincipalname);
                    command.AddParameter("path", user.path);
                    if (user.ipphone != null)
                    {
                        Hashtable attrHash = new Hashtable
                        {
                            { "ipPhone", user.ipphone }
                        };
                        command.AddParameter("OtherAttributes", attrHash);
                    }
                    powershell.Commands = command;
                    powershell.Invoke();
                    if (powershell.Streams.Error.Count > 0)
                    {
                        throw powershell.Streams.Error[0].Exception;
                    }
                    powershell.Streams.ClearStreams();

                    bool   adFinished            = false;
                    int    count                 = 0;
                    String objectNotFoundMessage = "Cannot find an object with identity";
                    while (adFinished == false && count < 6)
                    {
                        try
                        {
                            command = new PSCommand();
                            command.AddCommand("get-aduser");
                            command.AddParameter("identity", user.samaccountname);
                            powershell.Commands = command;
                            Collection <PSObject> check = powershell.Invoke();
                            if (powershell.Streams.Error.Count > 0)
                            {
                                if (powershell.Streams.Error[0].Exception.Message.Contains(objectNotFoundMessage))
                                {
                                    System.Threading.Thread.Sleep(3000);
                                }
                                else
                                {
                                    throw powershell.Streams.Error[0].Exception;
                                }
                            }
                            powershell.Streams.ClearStreams();
                            if (check.FirstOrDefault() != null)
                            {
                                adFinished = true;
                            }
                            count++;
                        }
                        catch (Exception e)
                        {
                            if (e.Message.Contains(objectNotFoundMessage))
                            {
                                System.Threading.Thread.Sleep(3000);
                                count++;
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }

                    if (count == 6)
                    {
                        throw new Exception("Retry count exceeded. May indicate account creation issue");
                    }
                }

                MSActorReturnMessageModel successMessage = new MSActorReturnMessageModel(SuccessCode, "");
                return(successMessage);
            }
            catch (Exception e)
            {
                if (!e.Message.Contains(accountExistsError))
                {
                    return(util.ReportError(e));
                }
                return(util.ReportHiddenError(e));
            }
        }