title: Why does authenticating against LDAP with DirectoryEntry intermittently throw COMException (0x8007203A): "The server is not operational"?
body: <p><strong>If anybody has a similar story, please post details below!</strong></p>
  
  <p>I'm building an ASP.NET website which needs to support authentication against LDAP.</p>
  
  <p>On windows, LDAP auth can be performed via Active Directory (I'm no expert, but AD seems to simply be a particular flavor of ldap).  I don't control the AD and/or LDAP servers.</p>
  
  <p>I've tried various methods of authentication, but I've settled on using a single <code>DirectoryEntry</code> per authentication attempt:</p>
  
  <pre><code>using (DirectoryEntry de = new DirectoryEntry(ldapPath, ldapUsername, password, AuthenticationTypes.ServerBind)) {
      try {
          // Bind to the native AdsObject to force authentication.
          object obj = de.NativeObject;//not IDisposable
      } catch(...
  </code></pre>
  
  <p>Retrieving the NativeObject causes a <code>COMException</code> if anything whatsoever goes wrong, for instance if the authentication failed, the exception is something like "Logon failure: unknown user name or bad password", and if the ldap server is unreachable or times out, something like "The server is not operational."</p>
  
  <p>This works, basically, but after variable number of days, always starting first thing in the morning, we get "The server is not operational." until IIS is restarted.  This is obviously not a great long-term solution, but as far as I can tell the fault lies with the Com Object underlying DirectoryEntry - not something easy to fix.</p>
  
  <p><a href="http://forums.asp.net/p/1051599/1631922.aspx" rel="noreferrer">This</a> <a href="http://forums.asp.net/t/1096070.aspx" rel="noreferrer">problem</a> <a href="http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_21021481.html" rel="noreferrer">isn't</a> <a href="http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic33330.aspx" rel="noreferrer">new</a> <a href="http://forums.asp.net/t/1020632.aspx" rel="noreferrer">or</a> <a href="http://forums.oracle.com/forums/thread.jspa?threadID=929309&amp;tstart=0" rel="noreferrer">unknown</a>.  Some people have gone through microsoft's support with mixed results; basically the answers seem to come down to "take your ldap path and create a few equivalent alternatives and maybe one of those will work".  Each time you try, or course, you won't know for a few days whether it actually worked, and until a real solution is found, we're back to "reboot the windows servers every night".</p>
  
  <p>As a start, I've tried ldap paths in the format</p>
  
  <pre><code>* "LDAP://server.uri:636"
  * "LDAP://insecure.server.uri:389"
  * "LDAP://server.uri:636/cn=username,ou=staff,o=myOrganisation,c=org"
  </code></pre>
  
  <p>Always with a username with the following pattern:</p>
  
  <pre><code>* "cn=username,ou=staff,o=myOrganisation,c=org"
  </code></pre>
  
  <p>All of these methods work initially, but fail after a variable number of days (and start working after an IIS reset). The server is running IIS6 on win 2k3. </p>
  
  <p>If anyone else has these problems, please post below, and perhaps eventually we'll find a pattern to work with or have sufficient number of examples to convince microsoft to fix this.</p>