title: .NET 4.5 Bug in UserPrincipal.FindByIdentity (System.DirectoryServices.AccountManagement)
body: <p>In testing our .NET 4.0 application under .NET 4.5, we've encountered a problem with the <code>FindByIdentity</code> method for <code>UserPrincipal</code>.  The following code works when run in a .NET 4.0 runtime, but fails under .NET 4.5:</p>
  
  <pre><code>[Test]
  public void TestIsAccountLockedOut()
  {
      const string activeDirectoryServer = "MyActiveDirectoryServer";
      const string activeDirectoryLogin = "MyADAccount@MyDomain";
      const string activeDirectoryPassword = "MyADAccountPassword";
      const string userAccountToTest = "TestUser@MyDomain";
      const string userPasswordToTest = "WRONGPASSWORD";
  
      var principalContext = new PrincipalContext(ContextType.Domain, activeDirectoryServer, activeDirectoryLogin, activeDirectoryPassword);
  
      var isAccountLockedOut = false;
      var isAuthenticated = principalContext.ValidateCredentials(userAccountToTest, userPasswordToTest, principalContext.Options);
      if (!isAuthenticated)
      {
          // System.DirectoryServices.AccountManagement.PrincipalOperationException : Information about the domain could not be retrieved (1355).
          using (var user = UserPrincipal.FindByIdentity(principalContext, IdentityType.UserPrincipalName, userAccountToTest))
          {
              isAccountLockedOut = (user != null) &amp;&amp; user.IsAccountLockedOut();
          }
      }
      Assert.False(isAuthenticated);
      Assert.False(isAccountLockedOut);
  }
  </code></pre>
  
  <p>Here is the exception stack trace:</p>
  
  <pre><code>System.DirectoryServices.AccountManagement.PrincipalOperationException : Information about the domain could not be retrieved (1355).
  at System.DirectoryServices.AccountManagement.Utils.GetDcName(String computerName, String domainName, String siteName, Int32 flags)   at System.DirectoryServices.AccountManagement.ADStoreCtx.LoadDomainInfo()   at 
  System.DirectoryServices.AccountManagement.ADStoreCtx.get_DnsDomainName()   at System.DirectoryServices.AccountManagement.ADStoreCtx.GetAsPrincipal(Object storeObject, Object discriminant)   at 
  System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType, String urnScheme, String urnValue, DateTime referenceDate, Boolean useSidHistory)   at 
  System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(Type principalType, String urnScheme, String urnValue, DateTime referenceDate)   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.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)   
  </code></pre>
  
  <p>Has anyone else seen and resolved this problem?  If not, is there a better way for us to check the <code>IsAccountLockedOut</code> status for an Active Directory account?</p>
  
  <p>For reference, all of our test machines are within the same subnet.  There are separate ActiveDirectory servers running Windows Server 2003, 2008 and 2012, in a variety of domain functional modes (see below).  The code works from machines running .NET 4.0, but fails from machines running .NET 4.5.</p>
  
  <p>The three .NET machines we ran the code from are:<br>
  - Windows 7 running .NET 4.0<br>
  - Windows Vista running .NET 4.5<br>
  - Windows Server 2012 running .NET 4.5  </p>
  
  <p>The Active Directory servers we've tried are:<br>
  - Windows 2003 with AD Domain Functional Mode set to Windows 2000 native<br>
  - Windows 2003 with AD Domain Functional Mode set to Windows Server 2003<br>
  - Windows 2008 with AD Domain Functional Mode set to Windows 2000 native<br>
  - Windows 2008 with AD Domain Functional Mode set to Windows Server 2003<br>
  - Windows 2008 with AD Domain Functional Mode set to Windows Server 2008<br>
  - Windows 2012 with AD Domain Functional Mode set to Windows 2012  </p>
  
  <p>All of those Active Directory servers are configured as a simple, single forest, and the client machines are not part of the domain.  They are not used for any other function than to test this behavior, and aren't running anything other than Active Directory.</p>
  
  <hr>
  
  <p>EDIT - 9 Oct 2012</p>
  
  <p>Thanks to everyone that replied.  Below is a C# command-line client that demonstrates the problem, and a short-term workaround that we identified that didn't require us to change anything about the Active Directory and DNS configurations.  It appears that the exception is only thrown once with an instance of the PrincipalContext.  We included the outputs for a .NET 4.0 machine (Windows 7) and a .NET 4.5 machine (Windows Vista).</p>
  
  <pre><code>using System;
  using System.DirectoryServices.AccountManagement;
  
  namespace ADBug
  {
      class Program
      {
          static void Main(string[] args)
          {
              const string activeDirectoryServer = "MyActiveDirectoryServer";
              const string activeDirectoryLogin = "MyADAccount";
              const string activeDirectoryPassword = "MyADAccountPassword";
              const string validUserAccount = "TestUser@MyDomain.com";
              const string unknownUserAccount = "UnknownUser@MyDomain.com";
  
              var principalContext = new PrincipalContext(ContextType.Domain, activeDirectoryServer, activeDirectoryLogin, activeDirectoryPassword);
  
              // .NET 4.0 - First attempt with a valid account finds the user
              // .NET 4.5 - First attempt with a valid account fails with a PrincipalOperationException
              TestFindByIdentity(principalContext, validUserAccount, "Valid Account - First Attempt");
              // Second attempt with a valid account finds the user
              TestFindByIdentity(principalContext, validUserAccount, "Valid Account - Second Attempt");
              // First attempt with an unknown account does not find the user
              TestFindByIdentity(principalContext, unknownUserAccount, "Unknown Account - First Attempt");
              // Second attempt with an unknown account does not find the user (testing false positive)
              TestFindByIdentity(principalContext, unknownUserAccount, "Unknown Account - Second Attempt");
              // Subsequent attempt with a valid account still finds the user
              TestFindByIdentity(principalContext, validUserAccount, "Valid Account - Third Attempt");
          }
  
          private static void TestFindByIdentity(PrincipalContext principalContext, string userAccountToTest, string message)
          {
              var exceptionThrown = false;
              var userFound = false;
              try
              {
                  using (var user = UserPrincipal.FindByIdentity(principalContext, IdentityType.UserPrincipalName, userAccountToTest))
                  {
                      userFound = (user != null);
                  }
              }
              catch (PrincipalOperationException)
              {
                  exceptionThrown = true;
              }
              Console.Out.WriteLine(message + " - Exception Thrown  = {0}", exceptionThrown);
              Console.Out.WriteLine(message + " - User Found = {1}", userAccountToTest, userFound);
          }
      }
  }
  </code></pre>
  
  <p>.NET 4.0 Output  </p>
  
  <pre><code>Valid Account - First Attempt - Exception Thrown  = False
  Valid Account - First Attempt - User Found = True
  Valid Account - Second Attempt - Exception Thrown  = False
  Valid Account - Second Attempt - User Found = True
  Unknown Account - First Attempt - Exception Thrown  = False
  Unknown Account - First Attempt - User Found = False
  Unknown Account - Second Attempt - Exception Thrown  = False
  Unknown Account - Second Attempt - User Found = False
  Valid Account - Third Attempt - Exception Thrown  = False
  Valid Account - Third Attempt - User Found = True
  </code></pre>
  
  <p>.NET 4.5 Output  </p>
  
  <pre><code>Valid Account - First Attempt - Exception Thrown  = True
  Valid Account - First Attempt - User Found = False
  Valid Account - Second Attempt - Exception Thrown  = False
  Valid Account - Second Attempt - User Found = True
  Unknown Account - First Attempt - Exception Thrown  = False
  Unknown Account - First Attempt - User Found = False
  Unknown Account - Second Attempt - Exception Thrown  = False
  Unknown Account - Second Attempt - User Found = False
  Valid Account - Third Attempt - Exception Thrown  = False
  Valid Account - Third Attempt - User Found = True
  </code></pre>