C#如何获取AD域所有的用户登录名

wonghoman 发布于 2013/03/07 23:15
阅读 4K+
收藏 0
急求代码,万分感谢!
加载中
0
Midnight
Midnight
private List<ADUsersEntity> SyncAdUsers()
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://OU=,DC=,DC=com");
            DirectorySearcher searcher = new DirectorySearcher(de, "(&(objectCategory=user)(sAMAccountName=*))");
            SearchResultCollection src = searcher.FindAll();
            List<ADUsersEntity> list = new List<ADUsersEntity>(10);
            foreach (SearchResult rs in src)
            {
                if (rs != null)
                {
                    string n = (rs.GetDirectoryEntry().Properties["distinguishedName"].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString();
                    if (n.IndexOf("OU") != -1)
                    {

                        string ADOrgName = (rs.GetDirectoryEntry().Properties["distinguishedName "].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["distinguishedName "].Value.ToString();
                        string ADAccount = (rs.GetDirectoryEntry().Properties["sAMAccountName"].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString();
                        string CNName = (rs.GetDirectoryEntry().Properties["name"].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["name"].Value.ToString();
                        string Email = (rs.GetDirectoryEntry().Properties["mail"].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["mail"].Value.ToString();
                        string DisplayName = (rs.GetDirectoryEntry().Properties["displayName "].Value == null) ? string.Empty : rs.GetDirectoryEntry().Properties["displayName"].Value.ToString();

                        if (string.IsNullOrEmpty(CNName) || CNName.Trim() == "")
                        {
                            CNName = DisplayName;
                        }
                        ADUsersEntity user = new ADUsersEntity();
                        user.CName = CNName;
                        user.ADAccount = ADAccount;
                        user.Email = Email;
                        user.ADOrgName = ADOrgName;
                        list.Add(user);
                    }
                }
            }
            return list;
        }
wonghoman
wonghoman
返回的list包含登录用户名?输出要遍历吗?怎样遍历?非常感谢你!
0
刘章明
刘章明
这个 是不是 太麻烦了呀 感觉效率 不高呀
返回顶部
顶部