/ Published in: C#
                    
                                        
Uses LDAP to find sAMAccountName by using the Email address.
Add System.DirectoryServices to references.
                Add System.DirectoryServices to references.
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
public static string GetSAMAccountNameByEmail(string email)
{
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();
}
Comments
 Subscribe to comments
                    Subscribe to comments
                
                