让所有用户从一组在Active Directory [英] get all users from a group in Active Directory

查看:129
本文介绍了让所有用户从一组在Active Directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去得到一个特定组的所有用户的广告,然后返回Emplyoyees列表作为映射到性能在我的Employee类。我有:

我的过滤器是不产生结果 - 是什么它应该是

此外,我尝试的第一个解决方案在这里: <一href="http://stackoverflow.com/questions/3195617/list-of-users-in-specific-active-directory-distribution-group">List在特定的Active Directory通讯组用户,但我需要的细节,如移动/扩展等...这我不能用这种方法得到的。

 公共静态列表&LT;员工&GT; CreateEmployeeList(字符串部)
    {
        名单&LT;员工&GT;员工=新的名单,其中,员工&GT;();
        字符串过滤器=的String.Format((及(对象类=人)(的memberOf = CN = {0},OU =用户和放大器;组,OU =胡说,DC =胡说,DC =胡说,DC =布拉赫)) , 部门);

        的DirectoryEntry adRoot =新的DirectoryEntry(LDAP://+域名,NULL,NULL,AuthenticationTypes.Secure);
        DirectorySearcher从搜索=新DirectorySearcher从(adRoot);
        sea​​rcher.SearchScope = SearchScope.Subtree;
        sea​​rcher.ReferralChasing = ReferralChasingOption.All;
        sea​​rcher.Filter =过滤器;
        SearchResultCollection结果= searcher.FindAll();

        的foreach(在结果信息搜索结果的用户)
        {
            //做任何你需要做的项

            如果(用户!= NULL)
            {
                UserDirectoryEntry = user.GetDirectoryEntry();
                字符串显示名= GetUserProperty(显示名);
                字符串的firstName = GetUserProperty(给定名称);
                字符串的lastName = GetUserProperty(SN);
                字符串email = GetUserProperty(邮件);
                串电话= GetUserProperty(telephonenumber);
                串延长= GetUserProperty(IPPHONE);
                串移动= GetUserProperty(手机);
                字符串标题= GetUserProperty(说明);
                employees.Add(新员工{全名=显示名,名字=名字,姓=姓氏,电子邮件= email.ToLower(),电话=电话,扩展=扩展,移动=移动,JOBTITLE =标题});
            }
        }
        返回的员工;
    }
 

解决方案

 使用(VAR上下文=新PrincipalContext(ContextType.Domain,则domainName))

            {
                使用(VAR组= GroupPrincipal.FindByIdentity(背景下,组名))
                {

                    如果(组== NULL)
                    {
                        的MessageBox.show(本集团不存在);
                    }
                    其他
                    {

                        VAR用户= group.GetMembers(真正的);
                        的foreach(在用户UserPrincipal用户)
                        {


                             //用户变量有关用户的详细信息

                        }
                     }
                  }
               }
 

Im trying to get all users of a particular group in AD, then return a list of Emplyoyees as mapped to properties in my Employee class. I have:

My Filter is producing no results - what should it be?

Also I tried the first solution here: List of users in specific Active Directory Distribution Group but i need details such as mobile/extension etc... which i couldnt get with that method.

public static List<Employee> CreateEmployeeList(string department)
    {
        List<Employee> employees = new List<Employee>();
        string filter = string.Format("(&(ObjectClass=person)(memberOf=CN={0},OU=Users & Groups,OU=Blah,DC=Blah,DC=Blah,DC=Blah))", department);

        DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
        DirectorySearcher searcher = new DirectorySearcher(adRoot);
        searcher.SearchScope = SearchScope.Subtree;
        searcher.ReferralChasing = ReferralChasingOption.All;
        searcher.Filter = filter;
        SearchResultCollection results = searcher.FindAll();

        foreach (SearchResult user in results)
        {
            // do whatever you need to do with the entry

            if (user != null)
            {
                UserDirectoryEntry = user.GetDirectoryEntry();
                string displayName = GetUserProperty("displayName");
                string firstName = GetUserProperty("givenName");
                string lastName = GetUserProperty("sn");
                string email = GetUserProperty("mail");
                string tel = GetUserProperty("telephonenumber");
                string extension = GetUserProperty("ipphone");
                string mobile = GetUserProperty("mobile");
                string title = GetUserProperty("description");
                employees.Add(new Employee{ FullName = displayName, FirstName = firstName, Surname = lastName, Email = email.ToLower(), Telephone = tel, Extension = extension, Mobile = mobile, JobTitle = title });
            }
        }
        return employees;
    }

解决方案

using (var context = new PrincipalContext(ContextType.Domain, "domainName"))

            {
                using (var group = GroupPrincipal.FindByIdentity(context, "groupName"))
                {

                    if (group == null)
                    {
                        MessageBox.Show("Group does not exist");
                    }
                    else
                    {

                        var users = group.GetMembers(true);
                        foreach (UserPrincipal user in users)
                        {


                             //user variable has the details about the user 

                        }
                     } 
                  }
               }

这篇关于让所有用户从一组在Active Directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆