title: LdapConnection SearchRequest throws exception for "The size limit was exceeded"
body: <p>Because of the fact that we are required to connect to an LDAP server using LDAPS we must use LdapConnection instead of DirectoryEntry.</p>
  
  <p>Here is the source code:</p>
  
  <pre><code>        SearchResponse response;
          using (LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier(Host, Port)))
          {
              if (IsSSL)
              {
                  con.SessionOptions.SecureSocketLayer = true;
                  con.SessionOptions.VerifyServerCertificate =
                      (connection, certificate)
                      =&gt; true;
              }
              con.Credential = new NetworkCredential(_username, _password);
              con.AuthType = AuthType.Basic;
              con.Bind();
  
              if (logMessage != null)
                  logMessage("Connected to LDAP");
  
              string sFilter = String.Format(
                  "(&amp;(objectcategory=person)(objectclass=user){0}(!(userAccountControl:1.2.840.113556.1.4.803:=2)))",
                  filter
                  );
  
              SearchRequest request = new SearchRequest("OU=Corp,DC=mydc,DC=com", sFilter, SearchScope.Subtree);
              request.Attributes.Add(Resources.objectguid);
              request.Attributes.Add(Resources.givenname);
              request.Attributes.Add(Resources.sn);
              request.Attributes.Add(Resources.initials);
              request.Attributes.Add(Resources.samaccountname);
              request.Attributes.Add(Resources.userprincipalname);
              request.Attributes.Add(Resources.mail);
              request.Attributes.Add(Resources.objectsid);
              request.Attributes.Add(Resources.department);
              request.Attributes.Add(Resources.company);
              request.SizeLimit = 10;
  
              response = (SearchResponse) con.SendRequest(request);
          }
  </code></pre>
  
  <p>Upon execution of the source code (we have verified credentials, host, port, etc - using an external 3rd party software) we get the following exception:</p>
  
  <h1>The size limit was exceeded</h1>
  
  <p><strong>Description:</strong> An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  
  <p><strong>Exception Details:</strong> System.DirectoryServices.Protocols.DirectoryOperationException: The size limit was exceeded</p>
  
  <p><strong>Source Error:</strong></p>
  
  <pre><code>response = (SearchResponse) con.SendRequest(request);
  </code></pre>
  
  <blockquote>
  <pre><code>[DirectoryOperationException: The size limit was exceeded]
     System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32
  </code></pre>
    
    <p>messageId, LdapOperation operation, ResultAll resultType, TimeSpan
    requestTimeOut, Boolean exceptionOnTimeOut) +2385
           System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest
    request, TimeSpan requestTimeout) +499
           System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest
    request) +50
           UserSearchProvider.ADUserSearchProvider.QueryStore(UserSearchCriteriaCollection
    criterias, Action<code>1 logMessage) in c:\Users\stemarie\Documents\Visual
    Studio
    2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\ADUserSearchProvider.cs:298
           UserSearchProvider.UserSearchProvider.QueryAndSort(UserSearchCriteriaCollection
    criterias, Action</code>1 logMessage) in c:\Users\stemarie\Documents\Visual
    Studio
    2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:77
           UserSearchProvider.UserSearchProvider.Search(UserSearchCriteriaCollection
    criterias, Action<code>1 logMessage) in c:\Users\stemarie\Documents\Visual
    Studio
    2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchProvider.cs:33
           UserSearchProvider.UserSearchService.Search(UserSearchCriteriaCollection
    criterias, Action</code>1 logMessage) in c:\Users\stemarie\Documents\Visual
    Studio
    2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProvider\UserSearchService.cs:44
           UserSearchProviderTest._Default.Page_Load(Object sender, EventArgs e) in c:\Users\stemarie\Documents\Visual Studio
    2012\Projects\Idealink.Modules\UserSearchProvider\UserSearchProviderTest\Default.aspx.cs:28</p>
  </blockquote>
  
  <p>The part that confuses me is that we did specify the maximum size limit, we don't want more than 100 entries - we want to limit it. But yet the library consistently throws the error even if we specify a SizeLimit of 1.</p>
  
  <p>Does anyone have any insights/suggestions regarding this issue? We are very close to getting this working and just need to resolve this last problem.</p>