.NET 4.5 Bug in UserPrincipal.FindByIdentity (System.DirectoryServices.AccountManagement)

20,598

Solution 1

To the OP (and anyone else that helped with replies) we have(had) the same exact issue. In our development environment, installed VS2012 and our app broke at runtime during login (AD issue as pointed out above). So I had my system wiped and continued using 2010, all the while shedding a tear every time Id read a new blog post about how awesome 2012 is blah blah.

So I found this thread thanks to Scott Hanselman. I installed a VM on my development box, Windows 8 developer 90day preview on it, and VS2012. Got our Application up and running and immediately was hit with the login AD snag. Simply wrapped our FindByIdentity in a try catch and forced it to try again after the first catch - and viola it works!! So thanks to whoever figured that little trick out!!

So, its a minor fix, and a "hack" that works for local development, and shouldn't affect production since we aren't putting 4.5 on production any time soon.

But the downside is that locally, logging in now takes like 2 minutes versus seconds when we ran under 2010 :(

I don't really know what else I can provide to actually try to help solve the situation, but figured Id share my 2 cents anyway since this still appears to be a major issue.

Solution 2

We are experiencing the exact same issue (cross domain queries failing on updating to 4.5) - I would consider this a bug since it breaks existing (4.0) code.

However, in the interest of making it work - taking a look at one of the (now) failing clients, I noticed that there were a bunch of DNS requests for SRV records that were failing, of the form:

_ldap._tcp.MYSERVER1.mydomain.com,INet,Srv
_ldap._tcp.dc._msdcs.mydomain.com,INet,Srv

Modifying our DNS server (the DNS used by the failing clients) to have a forward zone for all mydomain.com traffic to one of the DCs on the domain did resolve the issue.

Using nslookup, the behavior from before (when it was failing) to now (working) was that before those queries would return "Non-existent domain" whereas now they return "* No Service location (SRV) records available for ...". The point of failure seems to be the perceived nonexistence of the domain rather than missing SRV records. Hopefully MS reverts this behavior but in the meantime you might have some luck creating a DNS forward zone if you can control the DNS for the failing clients.

Solution 3

Had same problem after upgrading .net framework from 4.0 to 4.5 I have ugpraded framework to .net 4.5.1 and it worked.

Share:
20,598
Grand Avenue Software
Author by

Grand Avenue Software

Updated on March 15, 2020

Comments

  • Grand Avenue Software
    Grand Avenue Software over 4 years

    In testing our .NET 4.0 application under .NET 4.5, we've encountered a problem with the FindByIdentity method for UserPrincipal. The following code works when run in a .NET 4.0 runtime, but fails under .NET 4.5:

    [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) && user.IsAccountLockedOut();
            }
        }
        Assert.False(isAuthenticated);
        Assert.False(isAccountLockedOut);
    }
    

    Here is the exception stack trace:

    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)   
    

    Has anyone else seen and resolved this problem? If not, is there a better way for us to check the IsAccountLockedOut status for an Active Directory account?

    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.

    The three .NET machines we ran the code from are:
    - Windows 7 running .NET 4.0
    - Windows Vista running .NET 4.5
    - Windows Server 2012 running .NET 4.5

    The Active Directory servers we've tried are:
    - Windows 2003 with AD Domain Functional Mode set to Windows 2000 native
    - Windows 2003 with AD Domain Functional Mode set to Windows Server 2003
    - Windows 2008 with AD Domain Functional Mode set to Windows 2000 native
    - Windows 2008 with AD Domain Functional Mode set to Windows Server 2003
    - Windows 2008 with AD Domain Functional Mode set to Windows Server 2008
    - Windows 2012 with AD Domain Functional Mode set to Windows 2012

    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.


    EDIT - 9 Oct 2012

    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).

    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 = "[email protected]";
                const string unknownUserAccount = "[email protected]";
    
                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);
            }
        }
    }
    

    .NET 4.0 Output

    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
    

    .NET 4.5 Output

    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
    
  • Grand Avenue Software
    Grand Avenue Software over 11 years
    Thanks for the feedback. We confirmed the same behavior (first attempt fails, all subsequent succeed) and posted a test client above. The last feedback we received from Microsoft was the following: "SDS.AM in .Net4.5 added a restriction to require domain DNS resolution on the client side. This was designed as DNS best practice should have correct DNS entry. Based on the feedback from the forum, we are considering to remove the restriction in the future release."
  • jkat98
    jkat98 over 11 years
    Thanks! One thing I also noticed is that after adding the first attempt failure catch - theres also a sizeable delay in any calls to AD - talking 3 minutes+. Are there entries to my local hosts I can add to fix this kind of issue? Thanks again for the feedback!
  • Grand Avenue Software
    Grand Avenue Software over 11 years
    I'm not sure if this is related, but we've always had significant performance issues with using hostnames (e.g. myserver) instead of IP addresses (e.g. 192.168.14.3) for the server. We haven't done any performance testing of the try-catch workaround for this problem, but I'll throw together a test case and see what kind of numbers we come up with.
  • Lin-Art
    Lin-Art over 11 years
    Nice one!! try-catch'ing the FindByIdentity() call twice worked out! -Thanks
  • mittal
    mittal almost 6 years
    I am having a similar issue with IsMemberOf() method. My .net version is 4.5.2 but DNS seems to be hard requirement, which my test machines could not fulfill. Any workaround ?
  • mittal
    mittal almost 6 years
    I started a new thread here stackoverflow.com/questions/52242019/…