2015-04-24 71 views
1

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

这适用于一些属性,如GivenName,Surname。但是当我试图获得像'名字'这样的属性值时,我得到了空值,而且我非常确定它们是用值填充而不是空的。

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

所以我的问题是,有人知道这是什么原因,当它不是一个权限问题。

在此先感谢

+0

调试它。当你得到一些用户检查UserPrincipal的所有属性。如果有任何属性被填入像GivenName或其他东西 - 比它被读取,所以如果某些属性为空 - 那么它真的是nul – MajkeloDev

回答

0

执行下面的代码,看看是所有属性的提供您的广告。还有就是你可能有一个变化拼错的关键或钥匙不在您的广告存在的所有属性

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 
[email protected] 
objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu 
dSCorePropagationData=1601-01-01 00:00:00 
lastLogonTimestamp=System.__ComObject 
[email protected] 
homePhone=1236456654654659 
mobile=800800800 
nTSecurityDescriptor=System.__ComObject 
+0

我会试着让你知道它是如何工作的。 – Basvo

+0

真的很奇怪,当使用你的方法时,我实际上获得了每个属性值。但是,当使用UserPrincipal时,我不会得到除SurName和GivenName之外的任何内容。虽然谢谢! – Basvo

+0

你可以发布你的代码吗? – Rad

1

我有同样的问题。我无法找出它们总是为空的真正原因。我的解决方法是将UserPrincipal转换为DirectoryEntry。然后您可以按名称调用属性。不理想,但它的工作原理。

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

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