I use this function to access LDAP and get user's email address attribute
public string Login(string userName, string password)
{
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://000.000.0.00"; // forexample
de.AuthenticationType = AuthenticationTypes.Secure;
de.Username = userName;
de.Password = password;
DirectorySearcher search = new DirectorySearcher(de);
search.Filter = "(&(samaccountname=" + userName + "))";
search.ReferralChasing = ReferralChasingOption.All;
SearchResult resultCol = search.FindOne();
string mailProperty="";
if (resultCol != null)
{
if (resultCol.Properties["mail"] != null && resultCol.Properties["mail"].Count > 0)
{
mailProperty = resultCol.Properties["mail"][0].ToString();
}
}
return mailProperty;
}
I didn't get mail attribute in the list of returned attributes, that is the list of attributes I got which doesn't contain the mail attribute
- givenname , samaccountname , cn ,
pwdlastset , whencreated , badpwdcount
, displayname , lastlogon , samaccounttype
, countrycode , objectguid , usnchanged
, whenchanged , name , objectsid
, logoncount , badpasswordtime , accountexpires
, primarygroupid , objectcategory , userprincipalname
, useraccountcontrol , description , dscorepropagationdata , distinguishedname , objectclass , usncreated , lastlogontimestamp , adspath , lastlogoff , instancetype , codepage , sn
(&(&(objectClass=user)(!(objectClass=computer)))(samaccountname=" + username + "))
– abydal