0
votes

Getting EmailAddress NULL from Active Directory (AccountManagement UserPrincipal)

Refering to the above thread, can anyone purpose the exact solution. I know my code is working fine with lots of Active Directory Servers but there is one Active Directory deployment causing this issue.

I have checked the user properties and email using LDAP query and explorer and it seems fine to me. Below is the code that works fine and fetches all email addresses for domain abc.com.pk.

DirectoryEntry entry = new DirectoryEntry("LDAP://abc.com.pk");
            DirectorySearcher dSearch = new DirectorySearcher(entry);
            dSearch.Filter = "(objectClass=user)";
            Console.WriteLine("Email addresses configure in your domain are");
            var allUsers =  dSearch.FindAll();
            foreach (SearchResult sResultSet in allUsers)
            {
                if (sResultSet.Properties["mail"].Count > 0)
                {
                    Console.WriteLine(i+": "+sResultSet.Properties["mail"][0]);
                    i++;
                }

            }

Here is my code that is not working and it should work fine.


    using (var principalContext = new PrincipalContext(ContextType.Domain | ContextType.Machine))
                            {
                                using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, identity.Name))
                                { 
var emailAddress = UserPrincipal.EmailAddress; // this is null
}}

Above answer made some sense but I need details if anyone can help. Thanks

1

1 Answers

0
votes

Issue that I have understood so far :

The web application running on IIS under a specific identity. This pool identity account doesn't has enough permissions to access AD.

Just change the permissions or change your pool identity to one of default i.e. NetworkServices or IdentityPool

Thanks