2011-07-13 31 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

对于你应该看看查询特定对象类的架构的所有可能的属性的列表。