2011-07-13 28 views

回答

14

指定「*」作爲要返回的屬性列表中的唯一值。

如果您還想要操作屬性,請將「+」添加到列表中。

+0

非常感謝你。 search.PropertiesToLoad.Add(「*」); search.PropertiesToLoad.Add(「+」); – DFTR

2

你可以使用一個DirectoryEntry來生成一個屬性列表,你當然必須使用每個屬性列表來查看。

DirectoryEntry objADAM = default(DirectoryEntry); 
    string properties = string.Empty; 
    foreach (string property in objADAM.Properties.PropertyNames) 
    { 
     properties += property + ", "; 
    } 

你總是可以但是參考 http://www.codeproject.com/KB/system/everythingInAD.aspx 當它涉及到C#和Active Directory。

UPDATE:http://www.codeproject.com/Articles/18102/Howto-Almost-Everything-In-Active-Directory-via-C

+3

是的 - 但是隻有**纔會獲得那些賦予它們值的屬性,對於那個特定的'DirectoryEntry'。這沒有**枚舉整個可能的屬性列表..... –

+0

'http:// www.codeproject.com/KB/system/everythingInAD.aspx'找不到 – Kiquenet

3

井「retreiving所有屬性」獨自一人,只要目錄擔心是沒有意義的。 你的意思是:

  1. 所有的用戶可能的屬性,因爲他們在模式discribed
  2. 重視所有的用戶屬性
  3. 所有的用戶和業務屬性

而且我不照顧某些用戶屬性可以是隻讀的,而其他的只能用特定的值寫入。我添加了獲取內容的方式。

@Ghostfire爲解決所有用戶屬性的價值和操作屬性提供瞭解決方案。

DirectoryEntry deUser = new DirectoryEntry("LDAP://WM2008R2ENT:389/CN=AUser,OU=MonOu,DC=dom,DC=fr"); 


foreach (string property in deUser.Properties.PropertyNames) 
{ 
    Console.WriteLine("\t{0} : {1} ", property, deUser.Properties[property][0]); 
} 

但請記住,在LDAP搜索時,最好的辦法是給你想中檢索的attributs:

/* Connection to Active Directory 
*/ 
DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr"); 

/* Directory Search 
*/ 
DirectorySearcher dsLookFor = new DirectorySearcher(deBase); 
dsLookFor.Filter = "(sn=users)"; 
dsLookFor.SearchScope = SearchScope.Subtree; 
dsLookFor.PropertiesToLoad.Add("cn"); 
dsLookFor.PropertiesToLoad.Add("givenName"); 
dsLookFor.PropertiesToLoad.Add("telephoneNumber"); 

dsLookFor.Sort = new SortOption("givenName", SortDirection.Descending); 
dsLookFor.VirtualListView = new DirectoryVirtualListView(1, 0, 2); 
SearchResultCollection srcUsers = dsLookFor.FindAll(); 
20

我抓住所有參數的清單我的DirectoryEntry類對象。我希望這將有助於:

objectClass = System.Object[] 
cn = Administrator 
sn = Kwiatek (Last name) 
c = PL (Country Code) 
l = Warszawa (City) 
st = Mazowieckie (Voivodeship) 
title = .NET Developer 
description = Built-in account for administering the computer/domain 
postalCode = 00-000 
postOfficeBox = Warszawa Ursynów 
physicalDeliveryOfficeName = Wojskowa Akademia Techniczna 
givenName = Piotr (First name) 
distinguishedName = CN=Administrator,CN=Users,DC=helpdesk,DC=wat,DC=edu 
instanceType = 4 
whenCreated = 2012-11-23 06:09:28 
whenChanged = 2013-02-23 13:24:41 
displayName = Piotr Kwiatek (Konto administratora) 
uSNCreated = System.__ComObject 
memberOf = System.Object[] 
uSNChanged = System.__ComObject 
co = Poland 
company = HELPDESK 
streetAddress = Kaliskiego 2 
wWWHomePage = http://www.piotr.kwiatek.org 
name = Administrator 
objectGUID = System.Byte[] 
userAccountControl = 512 
badPwdCount = 0 
codePage = 0 
countryCode = 616 
badPasswordTime = System.__ComObject 
lastLogoff = System.__ComObject 
lastLogon = System.__ComObject 
logonHours = System.Byte[] 
pwdLastSet = System.__ComObject 
primaryGroupID = 513 
objectSid = System.Byte[] 
adminCount = 1 
accountExpires = System.__ComObject 
logonCount = 178 
sAMAccountName = Administrator 
sAMAccountType = 805306368 
objectCategory = CN=Person,CN=Schema,CN=Configuration,DC=helpdesk,DC=wat,DC=edu 
isCriticalSystemObject = True 
dSCorePropagationData = System.Object[] 
lastLogonTimestamp = System.__ComObject 
mail = [email protected] 
nTSecurityDescriptor = System.__ComObject 

,在這裏你有代碼:

string currentUserSid = WindowsIdentity.GetCurrent().User.Value; 

      PrincipalContext ctx = new PrincipalContext(
       ContextType.Domain, 
       "helpdesk.wat.edu"); 

      UserPrincipal up = UserPrincipal.FindByIdentity(
       ctx, IdentityType.Sid, 
       currentUserSid); 

      /* 
      * 
      */ 
      DirectoryEntry entry = up.GetUnderlyingObject() as DirectoryEntry; 
      PropertyCollection props = entry.Properties; 

      /* 
      * 
      */ 
      foreach (string propName in props.PropertyNames) 
      { 
       if (entry.Properties[propName].Value != null) 
       { 
        Console.WriteLine(propName + " = " + entry.Properties[propName].Value.ToString()); 
       } 
       else 
       { 
        Console.WriteLine(propName + " = NULL"); 
       } 
      } 


      Console.ReadKey(); 
+1

如何獲取'系統的值。對象[]','System .__ ComObject','System.Byte []'等等***屬性***? – Kiquenet

4
// This will list ALL the properties from AD (between 200 and 800..or more) 
    // If someone has a solution for non AD servers please post it! 

    List<String> properties = new List<String>(); 
    IPAddress[] ips = Dns.GetHostAddresses(Server).Where(w => w.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToArray(); 
    if (ips.Length > 0) 
    { 
     DirectoryContext directoryContext = new DirectoryContext(DirectoryContextType.DirectoryServer, ips[0].ToString() + ":389", Username, Password); 
     ActiveDirectorySchema adschema = ActiveDirectorySchema.GetSchema(directoryContext); 
     ActiveDirectorySchemaClass adschemaclass = adschema.FindClass("User"); 

     // Read the OptionalProperties & MandatoryProperties 
     ReadOnlyActiveDirectorySchemaPropertyCollection propcol = adschemaclass.GetAllProperties(); 

     foreach (ActiveDirectorySchemaProperty schemaProperty in propcol) 
      properties.Add(schemaProperty.Name.ToLower()); 
    } 
+0

哪個***命名空間用於'DirectoryContext'? – Kiquenet

+0

using System.DirectoryServices.ActiveDirectory; –

0

對於你應該看看查詢特定對象類的架構的所有可能的屬性的列表。