2015-04-24 72 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());