2

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);
        }

    }
Bill Greer
  • 3,046
  • 9
  • 49
  • 80
  • 1
    Maybe this will help: https://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object – jlavallet Apr 10 '18 at 21:04
  • You need to get the underlying [`DirectoryEntry`](https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry(v=vs.110).aspx) using [`_user.GetUnderlyingObject()`](https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principal.getunderlyingobject(v=vs.110).aspx). You can then iterate through the properties or sub-objects. – Ron Beyer Apr 10 '18 at 21:22

2 Answers2

2

It's not a dynamic property. EmailAddress is a property of the UserPrincipal class, which inherits from Principal. Your result is actually of type UserPrincipal, which is why you see the property when you debug, but you are accessing it as type Principal, which doesn't have a property called EmailAddress, so it is not available to you in your code.

If you want access to the EmailAddress property, you need to cast your result to UserPrincipal. Since you are sure all of your results will be user objects, then you can do that in the foreach:

foreach (UserPrincipal result in results)
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
1

You will want to get the DirectoryEntry object from the Principal and query it's properties.

Something like this - assuming email is stored in the 'mail' attribute.

 var directoryEntry = result.GetUnderlyingObject() as DirectoryEntry;
 if (directoryEntry != null && directoryEntry.Properties.Contains("mail"))
 {
    _user.EmailAddress = directoryEntry.Properties[property].Value.ToString();
 }

Here is an extension method that uses the above. It simply takes a string for the attribute that you are searching for.

public static string GetPropertyValue(this Principal principal, string property)
{
    var directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
    if (directoryEntry != null && directoryEntry.Properties.Contains(property))
    {
        return directoryEntry.Properties[property].Value.ToString();
    }

    return null;
}
Derek Hewitt
  • 775
  • 8
  • 16