Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

i have an application where its searching all domain users from AD, can some one let me know how can i search in multiple domains?
Posted

Great. You can fetch the list of multiple Active Directory (which u belongs) using the below code:

VB
On Error Resume Next
Const E_ADS_PROPERTY_NOT_FOUND  = &h8000500D

Set objUser = GetObject _
    ("LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com")

intPrimaryGroupID = objUser.Get("primaryGroupID")
arrMemberOf = objUser.GetEx("memberOf")

If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
    WScript.Echo "The memberOf attribute is not set."
Else
    WScript.Echo "Member of: "
    For Each Group in arrMemberOf
        WScript.Echo Group
    Next
End If
 
Share this answer
 
C#
public static DataTable SearchADUsers(string userName)
{
    //Create new datatable
    using (DataTable dtUsers = new DataTable())
    {
        dtUsers.Locale = CultureInfo.InvariantCulture;
        dtUsers.Columns.Add("key");
        dtUsers.Columns.Add("value");

        foreach (string domainToBeSearched in ConfigurationDetails.DomainTobeChecked.Split(','))
        {
            ValidateADUser(domainToBeSearched, userName, dtUsers);
        }
        return dtUsers;
    }
}



C#
private static void ValidateADUser(string domainToBeSearched, string userName, DataTable dtUsers)
        {
            try
            {
                using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainToBeSearched))
                {
                    using (DirectorySearcher searcher = new DirectorySearcher())
                    {
                        //Set root for searching
                        searcher.SearchRoot = domain;
                        //Set filters for searching user
                        searcher.Filter = FormFilter("user", userName + "*");
                        searcher.SearchScope = SearchScope.Subtree;
                        searcher.PageSize = 10;
                        //Get all registered user on basis of passed filter
                        using (SearchResultCollection adResult = searcher.FindAll())
                        {
                            DataRow dtrUsers = null;
                            foreach (SearchResult result in adResult)
                            {
                                if (dtUsers.Select("key='" + result.Properties["samaccountname"][0].ToString() + "'").Length <= 0)
                                {
                                    //Add user to datatable
                                    dtrUsers = dtUsers.NewRow();
                                    dtrUsers[0] = result.Properties["samaccountname"][0].ToString();
                                    dtrUsers[1] = result.Properties["DisplayName"][0];
                                    dtUsers.Rows.Add(dtrUsers);
                                }

                            }
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                LogUtil.WriteLog(LogLevel.ERROR, ex.ToString());
            }
        }



Specify the domain names in config file

<add key="DomainTobeChecked" value="abc.com, lmn.com,xyz.net">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900