Click here to Skip to main content
16,010,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
Iam using PricipalContext and GroupPrincipal object to get all the groups from the domain..
GroupPrincipal object returns as null. so PrincipalSearcher return an exception as below.

Exception : Logon failure: unknown user name or bad password
.

Here is my code.. Plz suggest me any way to get all the groups from Domain..
Thanks in advance..

C#
public void GetAllGroups()
    {
        try
        {
            PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU);
            GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);
            PrincipalSearcher srch = new PrincipalSearcher(oGroupPrincipal);
            PrincipalSearchResult<principal> searchResCol = srch.FindAll();

            //foreach (var found in srch.FindAll())
            //{
            //Code to get group from groups..
            //}
        }
        catch (Exception ex)
        {
        }
    }</principal>
Posted
Updated 6-Jun-13 19:40pm
v3
Comments
David_Wimbley 7-Jun-13 10:29am    
Im pretty sure you have to authenticate to the domain in some fashion in order to get information about it.

 
Share this answer
 
public void GetAllGroups()
    {
        try
        {
            StringBuilder strBuilder = new StringBuilder();
            
            DirectoryEntry de = new DirectoryEntry("activedirectorypath", "adminUserName", "adminPassword");
            DirectorySearcher deSearch = new DirectorySearcher();
            deSearch.SearchRoot = de;
            deSearch.Filter = "(&(ObjectClass=group))";//ObjectClass=user to get all users in active directory.
            deSearch.PropertiesToLoad.Add("name");//i.e Properties such as SamAccountName,Email,GivenName etc..,

            SearchResultCollection results = deSearch.FindAll();

            foreach (SearchResult res in results)
            {
                DisplayProperties("name", res, strBuilder);
            }
            lblGroups.Text = strBuilder.ToString();
        }
        catch (Exception ex)
        {
        }
    }


C#
private void DisplayProperties(string property, SearchResult res, StringBuilder strBuilder)
    {
        if (strBuilder.ToString() != "")
        {
            strBuilder.Append("<br/>");
        }
        ResultPropertyValueCollection col = res.Properties[property];
        foreach (object o in col)
        {
           strBuilder.Append(o.ToString());
        }
    }
 
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