C# System.DirectoryServices.Protocol搜索问题

C# System.DirectoryServices.Protocol搜索问题,c#,ldap,directoryservices,adldap,C#,Ldap,Directoryservices,Adldap,我正在尝试将搜索从System.DirectoryServices重新写入System.DirectoryServices.Protocol 在S.DS中,我得到了所有请求的属性,但在S.DS.p中,我没有得到GUID或HomePhone 它的其余部分只为一个用户工作 有什么想法吗 public static List<AllAdStudentsCV> GetUsersDistinguishedName( string domain, string distinguishedName

我正在尝试将搜索从System.DirectoryServices重新写入System.DirectoryServices.Protocol

在S.DS中,我得到了所有请求的属性,但在S.DS.p中,我没有得到GUID或HomePhone

它的其余部分只为一个用户工作

有什么想法吗

public static List<AllAdStudentsCV> GetUsersDistinguishedName( string domain, string distinguishedName )
        {
            try
            {

                NetworkCredential credentials               = new NetworkCredential( ConfigurationManager.AppSettings[ "AD_User" ], ConfigurationManager.AppSettings[ "AD_Pass" ] ); 
                LdapDirectoryIdentifier directoryIdentifier = new LdapDirectoryIdentifier( domain+":389" ); 

                using ( LdapConnection connection           = new LdapConnection( directoryIdentifier, credentials ) )
                {

                    SearchRequest searchRequest = new SearchRequest( );
                    searchRequest.DistinguishedName = distinguishedName;
                    searchRequest.Filter = "(&(objectCategory=person)(objectClass=user)(sn=Afcan))";//"(&(objectClass=user))";
                    searchRequest.Scope = SearchScope.Subtree;
                    searchRequest.Attributes.Add("name");
                    searchRequest.Attributes.Add("sAMAccountName");
                    searchRequest.Attributes.Add("uid");
                    searchRequest.Attributes.Add("telexNumber"); // studId
                    searchRequest.Attributes.Add("HomePhone"); //ctrId
                    searchRequest.SizeLimit = Int32.MaxValue;
                    searchRequest.TimeLimit = new TimeSpan(0, 0, 45, 0);// 45 min - EWB

                    SearchResponse searchResponse = connection.SendRequest(searchRequest) as SearchResponse;

                    if (searchResponse == null) return null;

                    List<AllAdStudentsCV> users = new List<AllAdStudentsCV>();

                    foreach (SearchResultEntry entry in searchResponse.Entries)
                    {
                        AllAdStudentsCV user = new AllAdStudentsCV();

                        user.Active = "Y";
                        user.CenterName = "";
                        user.StudId = GetstringAttributeValue(entry.Attributes, "telexNumber");
                        user.CtrId = GetstringAttributeValue(entry.Attributes, "HomePhone");
                        user.Guid = GetstringAttributeValue(entry.Attributes, "uid");
                        user.Username = GetstringAttributeValue(entry.Attributes, "sAMAccountName");

                        users.Add(user);
                    }

                    return users;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
公共静态列表getUsersDistingueISHEDNAME(字符串域,字符串区分名称)
{
尝试
{
NetworkCredential credentials=新的NetworkCredential(ConfigurationManager.AppSettings[“AD_User”]、ConfigurationManager.AppSettings[“AD_Pass”]);
LdapDirectoryIdentifier directoryIdentifier=新的LdapDirectoryIdentifier(域+“:389”);
使用(LdapConnection连接=新的LdapConnection(目录标识符、凭据))
{
SearchRequest SearchRequest=newsearchrequest();
searchRequest.DifferentizedName=DifferentizedName;
searchRequest.Filter=“(&(objectCategory=person)(objectClass=user)(sn=Afcan));/”(&(objectClass=user));
searchRequest.Scope=SearchScope.Subtree;
searchRequest.Attributes.Add(“名称”);
searchRequest.Attributes.Add(“sAMAccountName”);
searchRequest.Attributes.Add(“uid”);
searchRequest.Attributes.Add(“电传号码”);//studId
searchRequest.Attributes.Add(“HomePhone”);//ctrId
searchRequest.SizeLimit=Int32.MaxValue;
searchRequest.TimeLimit=newtimespan(0,0,45,0);//45分钟-EWB
SearchResponse SearchResponse=connection.SendRequest(searchRequest)作为SearchResponse;
if(searchResponse==null)返回null;
列表用户=新列表();
foreach(searchResponse.Entries中的SearchResultEntry条目)
{
AllAdStudentsCV user=新建AllAdStudentsCV();
user.Active=“Y”;
user.CenterName=“”;
user.StudId=GetstringAttributeValue(entry.Attributes,“电传号码”);
user.CtrId=GetstringAttributeValue(entry.Attributes,“HomePhone”);
user.Guid=GetstringAttributeValue(entry.Attributes,“uid”);
user.Username=GetstringAttributeValue(entry.Attributes,“sAMAccountName”);
用户。添加(用户);
}
返回用户;
}
}
捕获(例外情况除外)
{
投掷;
}
}
另外,如果我想获取AD中的每个用户,这样我就可以将数据与我的SQL DB同步,我该怎么做呢?我不断地得到超过最大大小的错误。我将大小设置为maxInt32。。。是否有“忽略大小”选项

谢谢


Eric-

我认为标准方法是使用System.DirectoryServices,而不是System.DirectoryServices.Protocol。为什么要使用后者

关于错误消息“已超出最大大小”的第二个问题,可能是因为您试图一次获取太多条目。
Active Directory限制查询返回的对象数,以避免目录过载(限制大约为1000个对象)。获取所有用户的标准方法是使用分页搜索。

算法如下:

  • 您可以构造将获取所有用户的查询
  • 您可以在此查询中指定一个特定控件(分页结果控件),指示这是 分页搜索,每页有500个用户
  • 启动查询,获取第一个页面并解析中的前500个条目 那一页
  • 你要求广告进入下一页,解析下500个条目
  • 重复此操作,直到没有剩余页面

  • 我认为标准方法是使用System.DirectoryServices,而不是System.DirectoryServices.Protocol。为什么要使用后者

    关于错误消息“已超出最大大小”的第二个问题,可能是因为您试图一次获取太多条目。
    Active Directory限制查询返回的对象数,以避免目录过载(限制大约为1000个对象)。获取所有用户的标准方法是使用分页搜索。

    算法如下:

  • 您可以构造将获取所有用户的查询
  • 您可以在此查询中指定一个特定控件(分页结果控件),指示这是 分页搜索,每页有500个用户
  • 启动查询,获取第一个页面并解析中的前500个条目 那一页
  • 你要求广告进入下一页,解析下500个条目
  • 重复此操作,直到没有剩余页面

  • S.AD.P应该快得多,我的第一次传球是用S.D。。。但取回需要15分钟。我们需要把所有的学生都叫来,所以我想佩奇是最好的选择。找到一些代码示例:好的,谢谢。S.DS.P似乎允许原始ldap访问,因为S.DS似乎依赖于ADSI和DCOM。今天我也学到了一些东西:)15分钟获取整个目录确实是很长的时间。即使有上万个条目,也只需要不到一分钟的时间(考虑到您对获取的属性进行了适当的筛选)。它解决了AD没有发回您请求的所有属性所面临的问题吗?是的,我复制的代码在我这边进行区分大小写的搜索,导致“HomePhone”出现问题,uid的含义发生了变化,不再是GUID,我不得不要求使用“objectGuid”来获取GUID。AD.P应该要快得多,我的第一次尝试是用s.DS实现的。。。但取回需要15分钟。我们需要把所有的学生都叫来,所以我想佩奇是最好的选择。找到一些代码sampl