Return to Snippet

Revision: 35508
at November 9, 2010 21:37 by poxet


Initial Code
public static string GetSAMAccountNameByEmail(string email)
{
    var oroot = new System.DirectoryServices.DirectoryEntry("LDAP://INVESTOR_AB");
    var osearcher = new System.DirectoryServices.DirectorySearcher(oroot);
    osearcher.Filter = string.Format("(&(mail={0}))", email);
    var oresult = osearcher.FindAll();

    if (oresult.Count == 0) throw new InvalidOperationException(string.Format("Cannot find mail {0} in LDAP.", email));
    if (oresult.Count > 1) throw new InvalidOperationException(string.Format("There are {0} items with mail {1} in LDAP.", oresult.Count, email));

    return oresult[0].Properties["sAMAccountName"][0].ToString();
}

Initial URL

                                

Initial Description
Uses LDAP to find sAMAccountName by using the Email address.

Add System.DirectoryServices to references.

Initial Title
Get sAMAccountName by Email from LDAP

Initial Tags
email

Initial Language
C#