最近我一直在研究一个非常大的应用程序的一小部分。在这部分中,我需要使用UserPrincipal类从活动目录属性接收数据。
这适用于某些属性,即GivenName,Surname。但是当我试图获取像'name'这样的属性的值时,我得到的是null值,而且我非常确定它们是用值填充的而不是null。
首先我认为这是一个权限问题,所以我要求管理员给我的帐户授予所有阅读权限,但他确实无法读取所有属性。但是我可以使用ActiveDirectoryExplorer应用程序读取它们。
所以我的问题是;当这不是许可问题时,有人知道这是什么原因。
提前致谢
答案 0 :(得分:1)
执行以下代码,查看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
答案 1 :(得分:1)
我遇到了同样的问题。我无法找出为什么这些始终为空的实际原因。我的解决方法是将UserPrincipal强制转换为DirectoryEntry。然后,您可以按名称调用属性。不理想,但它有效。
请注意,这些属性中的实际缺失(null)值将导致异常,因此需要进行处理。
//UserPrincipal user...
DirectoryEntry d = (DirectoryEntry)user.GetUnderlyingObject();
Console.WriteLine(d.Properties["GivenName"].Value.ToString() + d.Properties["sn"].Value.ToString());