使用 Active Directory 来自 UserPrincipal 的电子邮件地址为空

Email address null from UserPrincipal using Active Directory

Getting EmailAddress NULL from Active Directory (AccountManagement UserPrincipal)

参考上述线程,任何人都可以找到确切的解决方案。我知道我的代码在许多 Active Directory 服务器上运行良好,但有一个 Active Directory 部署导致了这个问题。

我已经使用 LDAP 查询和资源管理器检查了用户属性和电子邮件,我觉得没问题。下面是工作正常并获取域 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++;
                }

            }

这是我的代码,但它不能正常工作。


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

以上回答有些道理,但如果有人可以提供帮助,我需要详细信息。 谢谢

目前我理解的问题:

IIS 上特定标识下的 Web 应用程序 运行。此池标识帐户没有足够的权限访问 AD。

只需更改权限或将您的池身份更改为默认身份之一,即 NetworkServices 或 IdentityPool

谢谢