c# – 分页AD查询有时失败

前端之家收集整理的这篇文章主要介绍了c# – 分页AD查询有时失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些代码(下面)每15分钟运行一次.有时它将无法使用以下错误查询AD:
  1. System.DirectoryServices.Protocols.DirectoryOperationException: The server does not support the control. The control is critical.
  2. at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request,TimeSpan requestTimeout)

>当它成功运行时,整个过程大约需要一分钟才能运行,AD查询大约需要30秒,32页.
>当它失败时,它始终在第一页上.
>就我所知,它似乎没有在一个模式中失败(总是在一天的不同时间).

在谷歌搜索错误后,我发现了两个SO问题(one,two),指向使用AuthType.Ntlm来解决问题.但这对我来说并没有解决. Another说检查服务器是否支持分页(确实如此).

关于为什么会发生这种情况的任何想法?

  1. var attributesToReturn = new[] {
  2. "givenName","sn","middleName","extensionAttribute8","department","sAMAccountName","userAccountControl"
  3. };
  4. var filter = "(&(objectclass=user)(!(objectclass=computer))(sn=*)(givenName=*)(extensionAttribute8=*)(|(sn=a*)(sn=b*)(sn=c*)(sn=d*)(sn=e*)(sn=f*)(sn=g*)(sn=h*)(sn=i*)(sn=j*)(sn=k*)(sn=l*)(sn=m*)(sn=n*)(sn=o*)(sn=p*)(sn=q*)(sn=r*)(sn=s*)(sn=t*)(sn=u*)(sn=v*)(sn=w*)(sn=x*)(sn=y*)(sn=z*)))";
  5. var currentBatch = 1;
  6. var searchRequest = new SearchRequest("DC=foo,DC=bar,DC=baz",filter,SearchScope.Subtree,attributesToReturn);
  7. var pageRequestControl = new PageResultRequestControl(500);
  8. searchRequest.Controls.Add(pageRequestControl);
  9.  
  10. using (var ldapConnection = new LdapConnection("server.foo.bar.baz"))
  11. {
  12. ldapConnection.Credential = new NetworkCredential("user","pass","domain");
  13. ldapConnection.Timeout = new TimeSpan(0,4,0);
  14. ldapConnection.AuthType = AuthType.Ntlm; // https://stackoverflow.com/a/14255413
  15.  
  16. while (true)
  17. {
  18. log.Debug("Fetching batch {0} from AD",currentBatch);
  19. var searchResponse = (SearchResponse)ldapConnection.SendRequest(searchRequest);
  20. var pageResultResponse = (PageResultResponseControl)searchResponse.Controls[0];
  21.  
  22. log.Debug("Parsing AD response for batch {0}",currentBatch);
  23. ParseResponse(_return,searchResponse,includeDisabled);
  24. if (pageResultResponse.Cookie.Length == 0)
  25. break;
  26. pageRequestControl.Cookie = pageResultResponse.Cookie;
  27. currentBatch++;
  28. }
  29. }

解决方法

这可能不是问题,因为它有时只会失败,但我每次都有这个错误而且必须设置
  1. ldapConnection.SessionOptions.ProtocolVersion=3

因为它可以工作.

猜你在找的C#相关文章