Microsoft has a general purpose KB article (Q316748) describing how to authenticate against Active Directory using the DirectoryEntry object. In their example they produce a username value by concatenating the domain name and username into the standard NetBIOS format(“domain\username”) and passing that as a parameter to the directory entry constructor:
string domainAndUsername = domain + @"\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
It recently came to our attention that the domain part of the username was being completely ignored and in multiple environments I’ve confirmed this behavior. The username and password are in fact being used, as authentication fails when they’re invalid, but any arbitrary value can be supplied for the domain name and authentication passes. At a glance I’d theorize this format works for WinNT based directory access but the domain part is ignored for LDAP.
A check on google shows many LDAP examples passing a “domain\username” value to the DirectoryEntry object so I’ve either messed something up in my configuration or there’s a lot of people confused by the KB article. Can anyone confirm this is the expected behavior or recommend a way to accept “domain\username” values and authenticate against Active Directory with them?
Thanks,
The short answer: When the
pathparameter of theDirectoryEntryconstructor contains an invalid domain name theDirectoryEntryobject will (after an unsuccessful search for the invalid domain in the forrest) attempt a fall back by dropping the domain part of theusernameparameter and attempt connection using the plain username (sAMAccountName).The long answer: If the domain name specified in the
usernameparameter is invalid but the user exists in the domain specified in thepathparameter the user will be authenticated (through the use of the fallback). However, if the user exists in another domain in the forrest than the one specified in thepathparameter authentication will only succeed when the domain part of theusernameparameter is included and correct.There are four different ways of specifying the username parameter when dealing with DirectoryEntry-objects:
Let me illustrate with an example:
In the example above domain.one is the forrest root domain and domain.two is in the same forrest as domain.one (but a different tree naturally).
So to answer your question: Authentication will always fail if the user in not in the domain that we’re connecting to and no or an invalid domain name is specified in the
usernameparameter.