Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am trying to get information from AD for a particular user. I wrote code for it but I am not sure if it's the right way to do the checking for mail property.

C#
var user = new User();
var entry = new DirectoryEntry(LDAPservername, domainAndUsername, password);
var search = new DirectorySearcher(entry);
search.Filter = "(AccountName=" + userId + ")";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("Name");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("memberof");
SearchResult result;
try
{
result = search.FindOne();
}

Type t = result.GetType();
var p = t.GetProperty("mail");

if(p==null)
{
user.Email = "jhghjfajfjafjaf@gmail.com";
}


I want to check this result object has mail propertyName or not. If not then I want to assign a new value [
jhghjfajfjafjaf@gmail.com
] to it. Please take a look

What I have tried:

I tried this

C#
Type t = result.GetType();
var p = t.GetProperty("mail");

if(p==null)
{
user.Email = "jhghjfajfjafjaf@gmail.com";
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900