2011-09-01 105 views
1

我想重新写从一个的System.DirectoryServices搜索System.DirectoryServices.ProtocolSystem.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; 
      } 
     } 

另外,如果我想获取在广告的每个用户,这样我就可以用我的SQL数据库同步数据,我怎么做,我一直得到最大规模突破,错误。我将大小设置为maxInt32 ...是否有“忽略大小”选项?

感谢,

Eric-

+0

改变“HOMEPHONE”到“HOMEPHONE”现在我回家的电话,但还是老样子没有GUID ...任何想法都能在这里找到,而不是“UID”叫什么名字? –

+1

它应该是objectGUID –

+1

要容易地找到属性名称(即使那些AD不会使用常规工具(如ldapbrowser)显示),您应该尝试使用ADSIEdit(DC上的adsiedit.msc)浏览目录 –

回答

1

我认为,标准的方法是使用的System.DirectoryServices,不System.DirectoryServices.Protocol。为什么你想用后者?

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

的算法是这样的:

  1. 您构建将获取所有用户查询
  2. 指定要在此查询特定控制(分页结果控制),表明这是 一个分页搜索,每页500个用户
  3. 您启动查询,获取第一页并解析 中的前500个条目,该页面为
  4. 您要求AD申请下一页,解析下一个500 en试图
  5. 重复,直到没有剩下的页面
+1

S.AD.P应该是更快,我的第一次传球被S.DS打击......但需要15分钟才能获得。我们需要抓取所有的学生,所以我猜想分页是最好的选择。找到一些代码示例: –

+0

http://dunnry.com/blog/CommentView,guid,1707c3e7-5395-45f4-8882-3a17f291934b.aspx –

+0

http://dunnry.com/blog/CategoryView,category,Protocols.aspx –