public void Core_SearchTestBadProperty()
        {
            string[]            properties = new string[] { "name", "objectGUID", "doesNotExist" };
            UserPrincipal       up         = Utility.CreateUser(workspaceName);
            SearchResultsObject results    = DirectoryServices.Search(workspaceName, @"(objectClass=User)", properties);

            Assert.That(results.Results[0].Properties["doesNotExist"], Is.Null);

            Utility.DeleteUser(up.DistinguishedName);
        }
        public void Core_SearchTestSuccess()
        {
            // Create Users
            UserPrincipal up1 = Utility.CreateUser(workspaceName);
            UserPrincipal up2 = Utility.CreateUser(workspaceName);
            UserPrincipal up3 = Utility.CreateUser(workspaceName);

            // Create Groups
            GroupPrincipal gp1 = Utility.CreateGroup(workspaceName);
            GroupPrincipal gp2 = Utility.CreateGroup(workspaceName);

            // Search For Users
            Console.WriteLine($"Searching For Users In [{workspaceName}].");
            string[]            properties = new string[] { "name", "objectGUID", "objectSid" };
            SearchResultsObject results    = DirectoryServices.Search(workspaceName, @"(objectClass=User)", properties);

            Assert.That(results.Results.Count, Is.EqualTo(3));
            foreach (SearchResultRow row in results.Results)
            {
                Console.WriteLine($"  >> [{row.Path}]");
                Assert.That(row.Properties.ContainsKey("name"), Is.True);
                Assert.That(row.Properties["name"], Is.Not.Null);
                Assert.That(row.Properties.ContainsKey("objectGUID"), Is.True);
                Assert.That(row.Properties["objectGUID"], Is.Not.Null);
                Assert.That(row.Properties.ContainsKey("objectSid"), Is.True);
                Assert.That(row.Properties["objectSid"], Is.Not.Null);
            }

            // Search For Groups
            Console.WriteLine($"Searching For Groups In [{workspaceName}].");
            results = DirectoryServices.Search(workspaceName, @"(objectClass=Group)", properties);
            Assert.That(results.Results.Count, Is.EqualTo(2));
            foreach (SearchResultRow row in results.Results)
            {
                Console.WriteLine($"  >> [{row.Path}]");
                Assert.That(row.Properties.ContainsKey("name"), Is.True);
                Assert.That(row.Properties["name"], Is.Not.Null);
                Assert.That(row.Properties.ContainsKey("objectGUID"), Is.True);
                Assert.That(row.Properties["objectGUID"], Is.Not.Null);
                Assert.That(row.Properties.ContainsKey("objectSid"), Is.True);
                Assert.That(row.Properties["objectSid"], Is.Not.Null);
            }

            // Delete Users
            Utility.DeleteUser(up1.DistinguishedName);
            Utility.DeleteUser(up2.DistinguishedName);
            Utility.DeleteUser(up3.DistinguishedName);

            // Delete Groups
            Utility.DeleteGroup(gp1.DistinguishedName);
            Utility.DeleteGroup(gp2.DistinguishedName);
        }
    private void ProcessSearchRequest(AdSearchRequest request)
    {
        ActiveDirectoryStatus status = new ActiveDirectoryStatus()
        {
            Action  = ActionType.Search,
            Status  = AdStatusType.Success,
            Message = "Success",
        };


        ActiveDirectoryObjectResult result = new ActiveDirectoryObjectResult()
        {
            Type = AdObjectType.SearchResults,
        };

        try
        {
            string searchBase = request.SearchBase;
            if (String.IsNullOrWhiteSpace(request.SearchBase))
            {
                searchBase = DirectoryServices.GetDomainDistinguishedName();
            }
            roleManager.CanPerformActionOrException(requestUser, ActionType.Search, searchBase);

            String filter = request.Filter;
            if (request.Parameters != null)
            {
                foreach (RegexParameters param in request.Parameters)
                {
                    filter = Regex.Replace(filter, param.Find, param.ReplaceWith);
                }
            }

            OnLogMessage("ProcessSearchRequest", $"Executing Search.  Filter String: [{filter}].  Search Base: [{searchBase}].");
            SearchResultsObject searchResults = DirectoryServices.Search(searchBase, filter, request.ReturnAttributes?.ToArray());
            result.Object = searchResults;
            result.Statuses.Add(status);
        }
        catch (AdException ade)
        {
            ProcessActiveDirectoryException(result, ade, ActionType.Search);
        }

        results.Add(result);
    }