返回 null 的用户主体属性



最近我一直在研究一个非常大的应用程序的一小部分。在这一部分中,我需要使用 UserPrincipal 类从活动目录属性接收数据。

这适用于某些属性,即给定名称、姓氏。但是当我尝试获取"name"等属性的值时,我得到的是空值,我非常非常确定它们充满了值而不是空值。

首先,我认为这是一个权限问题,所以我要求管理员向我的帐户授予所有读取权限,他做到了,但我仍然无法读取所有属性。但是我可以使用ActiveDirectoryExplorer应用程序阅读它们。

所以我的问题是;当这不是权限问题时,有人知道这是什么原因吗?

提前致谢

执行以下代码,查看 AD 中可用的所有属性。有一个更改,您可能拼错了密钥或 AD 中不存在该密钥

DirectorySearcher mySearcher = new System.DirectoryServices.DirectorySearcher(entry);
            foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
            {
                try
                {
                    foreach (string property in resEnt.Properties.PropertyNames)
                    {
                        string value = resEnt.Properties[property][0].ToString();
                        Console.WriteLine(property + ":" + value);
                    }
                }
                catch (Exception)
                { }
            }

我的 Windows Server 2008 R2 AD 中的属性列表

objectClass=top;person;organizationalPerson;user
cn=x1
sn=LastName
c=PL
l=City
st=State
title=Job title
description=Description
postalCode=Zip
postOfficeBox=POBox
physicalDeliveryOfficeName=Office
telephoneNumber=123456779
givenName=FirstName
distinguishedName=CN=x1,CN=Users,DC=helpdesk,DC=wat,DC=edu
instanceType=4
whenCreated=2012-11-27 21:37:37
whenChanged=2012-12-11 21:33:51
displayName=DisplayName
uSNCreated=System.__ComObject
uSNChanged=System.__ComObject
co=Poland
department=Department
company=Company
streetAddress=Street
name=x1
objectGUID=System.Byte[]
userAccountControl=66048
badPwdCount=0
codePage=0
countryCode=616
badPasswordTime=System.__ComObject
lastLogoff=System.__ComObject
lastLogon=System.__ComObject
pwdLastSet=System.__ComObject
primaryGroupID=513
objectSid=System.Byte[]
accountExpires=System.__ComObject
logonCount=1
sAMAccountName=x1
sAMAccountType=805306368
userPrincipalName=x1@helpdesk.wat.edu
objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu
dSCorePropagationData=1601-01-01 00:00:00
lastLogonTimestamp=System.__ComObject
mail=mail@mail.com
homePhone=1236456654654659
mobile=800800800
nTSecurityDescriptor=System.__ComObject
<</div> div class="one_answers">

我遇到了同样的问题。我无法找出这些总是为空的实际原因。我的解决方法是将UserPrincipal强制转换为DirectoryEntry。然后,可以按名称调用属性。不理想,但它有效。

请注意,这些属性中实际缺失的 (null) 值将导致异常,因此需要处理。

     //UserPrincipal user...
     DirectoryEntry d = (DirectoryEntry)user.GetUnderlyingObject();
     Console.WriteLine(d.Properties["GivenName"].Value.ToString() + d.Properties["sn"].Value.ToString());

最新更新