Usually, we use a LDAP search filter to find a user. The filter like"(Samaccountname = user alias)".
But there is a special situation is: the domain what the LDAP path assign has several sub-domains, and there are several users of these sub-domains have same alias.
E. g.
The LDAP path is" GC: // dc = root, Dc = com ".
There are two sub-domains of root domain:
GC: // dc = sub1, Dc = root, Dc = com
GC: // dc = sub2, Dc = root, Dc = com
There are two users are:
Sub1 \ Username
Sub2 \ Username
We cannot write a filter like" (! (Domainname = Domain Name) (samaccountname = user alias )) ". If we use the filter" (Samaccountname = user alias) "To search from" GC: // dc = root, Dc = com ", We will get two results.
Which is our real target?
So, we must identify the distinguishedname of the user with regular expression"DC * = [^,] +"To get the use's domain name.
SearchresultcollectionResults = mysearcher. findall ();
// Check domain name
Foreach ( Searchresult Result In Results)
{
StringDN = result. properties ["Distinguishedname"] [0]. tostring ();
Match M = RegEx . Match (DN, "DC * = [^,] +" );
String Mvalue = RegEx . Replace (M. value, @ "[\ S]" , "" , Regexoptions . Ignorecase );
If ( String . Compare (mvalue, "DC =" + Userdomain, True ) = 0)
{
// The target is found
}
}