2
回答
C#如何获取AD域所有的用户登录名
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
急求代码,万分感谢!
<无标签>
举报
wonghoman
发帖于5年前 2回/4K+阅
共有2个答案 最后回答: 4年前
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;
        }
--- 共有 1 条评论 ---
wonghoman返回的list包含登录用户名?输出要遍历吗?怎样遍历?非常感谢你! 5年前 回复
顶部