I'm trying to check if a user exists in Active Directory before creating it. I'm using the following code:
private static DirectoryEntry FindActiveDirectoryUser(string userName, string domainName)
{
using (DirectoryEntry domain = new DirectoryEntry("LDAP://" + domainName))
{
using (DirectorySearcher searcher = new DirectorySearcher(domain))
{
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.Filter = "(sAMAAccountName=" + userName + ")";
return searcher.FindOne().GetDirectoryEntry();
}
}
}
I'm getting the error
A referral was returned from the server.
for the variables userName and domainName, I tried both FQDN and pre-2000 username (e.g. DOMAIN\User), as well as simple domain and user names.
Does anyone know how to resolve this?