search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne();
string fullname = (string)result.Properties["name"][0];
string uname = (string)result.Properties["cn"][0];
string firstname = (string)result.Properties["givenname"][0];
string lastname = (string)result.Properties["sn"][0];
string mmail = (string)result.Properties["mail"][0];
When I tested with several user accounts, some authenticated perfectly fine,
some threw the exception "Index was out of range. Must be non-negative and
less than the size of the collection. Parameter name: index" . The
exception was eventually traced to the " string mmail =
(string)result.Properties["mail"][0]; " statement. So is this because that
user has no email in AD? How best to handle this?
Am a newbie in this area. Any insight appreciate.
Check it like this
if (searchResult.Properties.Contains("mail"))
{
...
}