/// <summary> Checks the resultCode and throws the appropriate exception. /// /// </summary> /// <exception> LdapException A general exception which includes an error /// message and an Ldap error code. /// </exception> /* package */ internal virtual void chkResultCode() { if (exception != null) { throw exception; } LdapException ex = ResultException; if (ex != null) { throw ex; } }
/// <summary> /// Collects batchSize elements from an LdapSearchQueue message /// queue and places them in a Vector. /// If the last message from the server, /// the result message, contains an error, it will be stored in the Vector /// for nextElement to process. (although it does not increment the search /// result count) All search result entries will be placed in the Vector. /// If a null is returned from getResponse(), it is likely that the search /// was abandoned. /// </summary> /// <param name="cancellationToken"></param> /// <returns> /// true if all search results have been placed in the vector. /// </returns> private async Task <bool> GetBatchOfResultsAsync(CancellationToken cancellationToken) { LdapMessage msg; // <=batchSize so that we can pick up the result-done message for (var i = 0; i < _batchSize;) { try { if ((msg = await _queue.GetResponse(cancellationToken).ConfigureAwait(false)) != null) { // Only save controls if there are some var ctls = msg.Controls; if (ctls != null) { ResponseControls = ctls; } if (msg is LdapSearchResult) { // Search Entry object entry = ((LdapSearchResult)msg).Entry; _entries.Add(entry); i++; _entryCount++; } else if (msg is LdapSearchResultReference) { // Search Ref var refs = ((LdapSearchResultReference)msg).Referrals; if (_cons.ReferralFollowing) { _referralConn = await _conn.ChaseReferralAsync(_queue, _cons, msg, refs, 0, true, _referralConn, cancellationToken).ConfigureAwait(false); } else { _references.Add(refs); _referenceCount++; } } else { // LdapResponse var resp = (LdapResponse)msg; var resultCode = resp.ResultCode; // Check for an embedded exception if (resp.HasException()) { // Fake it, results in an exception when msg read resultCode = LdapException.ConnectError; } if (resultCode == LdapException.Referral && _cons.ReferralFollowing) { // Following referrals _referralConn = await _conn.ChaseReferralAsync(_queue, _cons, resp, resp.Referrals, 0, false, _referralConn, cancellationToken : cancellationToken).ConfigureAwait(false); } else if (resultCode != LdapException.Success) { // Results in an exception when message read _entries.Add(resp); _entryCount++; } // We are done only when we have read all messages // including those received from following referrals var msgIDs = _queue.MessageIDs; var controls = _cons.GetControls(); if (msgIDs.Length == 0 && (controls == null || controls.Length == 0)) { // Release referral exceptions await _conn.ReleaseReferralConnections(_referralConn, cancellationToken).ConfigureAwait(false); return(true); // search completed } } } else { // We get here if the connection timed out // we have no responses, no message IDs and no exceptions var e = new LdapException(null, LdapException.LdapTimeout, null); _entries.Add(e); break; } } catch (LdapException e) { // Hand exception off to user _entries.Add(e); } } return(false); // search not completed }
private void HandleLdapException(LdapException x) { switch (x.ResultCode) { case LdapException.Ldap_TIMEOUT: throw new TimeoutException("Ldap lookup timed out", x); case LdapException.OPERATIONS_ERROR: case LdapException.INVALID_DN_SYNTAX: if (x.ResultCode == 1 && x.LdapErrorMessage.Contains("DSID-0C090627")) throw new DreamAbortException(DreamMessage.Forbidden(string.Format("Account '{0}' is disabled", this._username))); throw new ArgumentException(string.Format("The search base '{0}' may have invalid format (Example: 'DC=sales,DC=acme,DC=com') or the account used for binding may be disabled. Error returned from LDAP: {1}", _config.LdapSearchBase, x.LdapErrorMessage), x); default: throw x; } }
protected void NotifyExceptionListeners(LdapMessage sourceMessage, LdapException ldapException) { if (null != directory_exception_event) { directory_exception_event(this, new DirectoryExceptionEventArgs(sourceMessage, ldapException)); } }