I am iterating through some Active Directory PrincipalSearcher Principal results using the code below. In my foreach loop where I am assigning result properties to my _user object I can break in debug and see a result.EmailAddress value. When I try to assign result.EmailAddress to _user.EmailAdress the code will not even compile. I'm guessing EmailAddress is a dynamic property of result. Is there a way to check for this property so I can add the users AD email address to my _user object?
private static void GetAllActiveDirectoryUsers()
{
PrincipalContext context = new PrincipalContext(
ContextType.Domain, Environment.UserDomainName);
UserPrincipal user = new UserPrincipal(context);
// create a principal searcher for running a search operation
PrincipalSearcher pS = new PrincipalSearcher(user);
// run the query
PrincipalSearchResult<Principal> results = pS.FindAll();
foreach (Principal result in results)
{
Console.WriteLine(result.DisplayName);
Console.ReadKey();
User _user = new User();
_user.Description = result.Description;
_user.DisplayName = result.DisplayName;
_user.DistinguishedName = result.DistinguishedName;
_user.Guid = result.Guid ?? null;
_user.Name = result.Name;
_user.Sid = result.Sid?.ToString();
Users.Add(_user);
}
}