Tonya
Tonya

Reputation: 11

LDAP DirectoryEntry connection suddenly failing

We've been successfully using the techniques described https://www.codemag.com/article/1312041/Using-Active-Directory-in-.NET for years. A few months ago we started getting "Server not operational" errors only when connecting to servers that were not using SSL (ie, connecting on port 389 not 636).

Our connection initially looked like:

using (HostingEnvironment.Impersonate())
            {
                DirectoryEntry entry = new DirectoryEntry(settings.Path, settings.UserName, settings.Password);
                DirectorySearcher searcher = new DirectorySearcher(entry, settings.ObjectClass, loadProps);

                searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
                searcher.SizeLimit = 0;
                searchResults = searcher.FindAll();
            }

After some research, it seemed like maybe we were exhausting the ports so we changed to the code below.

using (HostingEnvironment.Impersonate())
            {
                using (DirectoryEntry rootEntry = new DirectoryEntry(
                    settings.Path,
                    settings.UserName,
                    settings.Password,
                    AuthenticationTypes.None))
                {
                    rootEntry.RefreshCache();
                    DirectoryEntry entry = new DirectoryEntry(settings.Path, settings.UserName, settings.Password, AuthenticationTypes.None);
                    DirectorySearcher searcher = new DirectorySearcher(entry, settings.ObjectClass, loadProps);

                    searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
                    searcher.SizeLimit = 0;
                    searchResults = searcher.FindAll();
                }
            }

If we don't specify the authentication type, it just hangs at rootEntry.RefreshCache(). If we AuthenticationType.None (basic authentication), we get an invalid password error. If we use AuthenticationType.Secure, it hangs. My understanding is that Secure is the default, so not specifying anything is the same thing, hence, the same result when specifying no AuthenticationType.

If we switch back to the original code, we're now experiencing the same problems with authentication.

Upvotes: 0

Views: 25

Answers (0)

Related Questions