2016-06-16 13 views
7

Ho un codice (sotto) che viene eseguito ogni 15 minuti. volte non riuscirà ad interrogare AD con il seguente errore:Query AD cercapersone a volte non riesce

System.DirectoryServices.Protocols.DirectoryOperationException: The server does not support the control. The control is critical. 
    at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout) 
  • Quando viene eseguito correttamente, l'intero processo dura circa un minuto per correre con la query ad prendendo circa 30 secondi con 32 pagine.
  • Quando fallisce, è sempre sulla prima pagina.
  • Non sembra fallire in un modello (sempre diversi momenti della giornata) per quanto posso dire.

Dopo googling tale errore, ho trovato due SO domande (one, two) quel punto ad usare AuthType.Ntlm per risolvere il problema. Questo non l'ha risolto per me però. Another dice di verificare se il server supporta il paging (lo fa).

Qualche idea sul perché questo potrebbe accadere?

var attributesToReturn = new[] { 
    "givenName", 
    "sn", 
    "middleName", 
    "extensionAttribute8", 
    "department", 
    "sAMAccountName", 
    "userAccountControl" 
}; 
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*)))"; 
var currentBatch = 1; 
var searchRequest = new SearchRequest("DC=foo,DC=bar,DC=baz", filter, SearchScope.Subtree, attributesToReturn); 
var pageRequestControl = new PageResultRequestControl(500); 
searchRequest.Controls.Add(pageRequestControl); 

using (var ldapConnection = new LdapConnection("server.foo.bar.baz")) 
{ 
    ldapConnection.Credential = new NetworkCredential("user", "pass", "domain"); 
    ldapConnection.Timeout = new TimeSpan(0, 4, 0); 
    ldapConnection.AuthType = AuthType.Ntlm; // https://stackoverflow.com/a/14255413 

    while (true) 
    { 
     log.Debug("Fetching batch {0} from AD", currentBatch); 
     var searchResponse = (SearchResponse)ldapConnection.SendRequest(searchRequest); 
     var pageResultResponse = (PageResultResponseControl)searchResponse.Controls[0]; 

     log.Debug("Parsing AD response for batch {0}", currentBatch); 
     ParseResponse(_return, searchResponse, includeDisabled); 
     if (pageResultResponse.Cookie.Length == 0) 
      break; 
     pageRequestControl.Cookie = pageResultResponse.Cookie; 
     currentBatch++; 
    } 
} 
+0

Hai provato a smaltire gli oggetti che stai creando? – Shago

+0

@Shago 'LdapConnection' è l'unico oggetto usa e getta che sto usando e che viene gestito tramite l'istruzione' using'. – Chris

+0

A quale affermazione viene lanciata quell'eccezione? –

risposta

0

Questo non può essere il problema poiché non solo per te a volte, ma ho avuto questo errore ogni volta e ha dovuto impostare

ldapConnection.SessionOptions.ProtocolVersion=3 

per farlo funzionare a tutti.