none
Active Directory security groups Search application optimization RRS feed

  • Question

  • I have a Web API method that returns the Active Directory security groups for the specific login user. The below code was working fine but it was taking so munch time nearly 45 sec to get the results

    DirectoryEntry root = GetDirectoryEntry()
      using (var groups = root.Children.Find("OU=Sample Security Groups"))
                            {
                                using (var directory = groups.Children.Find("OU=Permissions"))
                                {
                                    using (var searcher = new DirectorySearcher(directory))
                                    {
                                        searcher.Filter = `filter condition`
                                        var results = searcher.FindAll();
                                        foreach (SearchResult result in results)
                                        {
                                            if (result != null)
                                            {
                                                using (DirectoryEntry group = result.GetDirectoryEntry())
                                                {
                                                    items.Add((string)group.Properties["sAMAccountName"].Value);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
    Can anyone help to optimize the code using Parallel.ForEach or threading etc..


    TARUN

    Thursday, October 17, 2019 3:33 AM

Answers

  • Hi TARUN00197,

    Thank you for posting here.

    According to your description, you want to optimize the code by using parallel.ForEach. You could convert foreach to Parallel.Foreach to do it.

    You could try the following code.

                var items = new List<string>();
                SearchResultCollection results = null ;
                using (var groups = root.Children.Find("OU=Sample Security Groups"))
                {
                    using (var directory = groups.Children.Find("OU=Permissions"))
                    {
                        using (var searcher = new DirectorySearcher(directory))
                        {
                            searcher.Filter = "filter condition";
                            results = searcher.FindAll();
                        }
                    }
                }
                List<SearchResult> list = new List<SearchResult>();
                foreach (SearchResult item in results)
                {
                    list.Add(item);
                }
                Parallel.ForEach(list,result=>
                {
                    if (result != null)
                    {
                        using (DirectoryEntry group = result.GetDirectoryEntry())
                        {
                            items.Add((string)group.Properties["sAMAccountName"].Value);
                        }
                    }
    
                });

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by TARUN00197 Friday, October 18, 2019 3:08 AM
    Thursday, October 17, 2019 6:03 AM
    Moderator