如何从Actvie目录获取所有组..没有用户名和密码..

问题描述:

嗨..

Iam使用 PricipalContext GroupPrincipal 对象获取所有来自域的组..

GroupPrincipal 对象返回null。所以 PrincipalSearcher 返回如下的异常。



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





这是我的代码.. Plz建议我从域中获取所有组的任何方式..

提前致谢..



.

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

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>

您好

查看以下链接以供参考。



http://stackoverflow.com/questions/637486/how-to-get-the-current-users-active-directory-details-in-c-sharp?rq= 1 [ ^ ]


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)
        {
        }
    }







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());
        }
    }