title: Active Directory authentication issue after reconnect - C#
body: <p><strong>adding more info on @serialhobbyist's request</strong><br><br>
  hi all. we're (= me and my colleagues) using PrincipalContext from System.DirectoryServices.AccountManagement to retrieve some authentication info (make sure a user belongs to a group etc).<br><br>
  <strong>Important: our client is a service running as LocalSystem. this does not happen when we run in a normal process or when we change the service to run as a specific user</strong><br></p>
  
  <p>here's a code sample:</p>
  
  <pre><code>using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain))
  {
      Principal p = Principal.FindByIdentity(ctx,IdentityType.Sid, sid);
      ...
  }
  </code></pre>
  
  <p>normally all works well (getting replies).
  once in a few hours (the trigger is not clear), or after a network disconnect and reconnect, (when our AD client disconnects, via network cable disconnect or VM adapter disable etc) we're getting this exception:</p>
  
  <pre><code>System.Runtime.InteropServices.COMException (0x80072020): An operations error occurred.
  
  at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
  at System.DirectoryServices.DirectoryEntry.Bind()
  at System.DirectoryServices.DirectoryEntry.get_AdsObject()
  at System.DirectoryServices.PropertyValueCollection.PopulateList()
  at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
  at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
  at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
  at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
  at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
  at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
  at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable``1 identityType, String identityValue, DateTime refDate)
  at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
  at System.DirectoryServices.AccountManagement.Principal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
  </code></pre>
  
  <p>searched for the exception (0x80072020), I see it translates to <code>LDAP_OPERATIONS_ERROR</code> (actually used as a code value translation example <a href="http://msdn.microsoft.com/en-us/library/aa746386%28VS.85%29.aspx" rel="nofollow noreferrer">here</a> :) ) 
  we inspected the traffic using wireshark and the exception is thrown before any LDAP query is sent.<br>
  after re-starting our process everything is back to normal (can validate successfully, no exceptions).
  can anybody shed light on the matter? any ideas how to achieve the equivalent the cleanups done on process termination?
  thanks!</p>
  
  <p>update: we've found a way around this, will supply answer below. however, we'd rather find a simpler solution.</p>