Csharp/CSharp Tutorial/Directory Services/Read — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

Get DirectoryEntry Path

using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de2 = new DirectoryEntry("LDAP://192.168.1.100/ou=auditing, dc=accounting, dc=ispnet1, dc=net");
      string newpath = de2.Path;
      Console.WriteLine("new path: {0}", newpath);
      de2.Close();
   }
}
new path: LDAP://192.168.1.100/ou=auditing, dc=accounting, dc=ispnet1, dc=net

Get MultiValued Attribute

using System;
using System.Runtime.InteropServices;
using System.DirectoryServices;
using ActiveDs;
public unsafe class MainClass 
{
  public static int Main(string[] args)
  {
    string Path = "LDAP://dsaddom.nttest.microsoft.ru/rootDSE";
    IADs pIADs = null;
    
    DirectoryEntry Entry = new DirectoryEntry(Path);
    pIADs = (IADs)Entry.NativeObject;
    Array NamingContexts = (Array)pIADs.Get("namingContexts");
    for(int i = 0; i < NamingContexts.GetLength(0); i++)
    {
      Console.WriteLine((string)NamingContexts.GetValue(i));
    }
    
    Array Contexts = (Array)pIADs.GetEx("namingContexts");
    for(int i = 0; i < NamingContexts.GetLength(0); i++)
    {
      Console.WriteLine((string)Contexts.GetValue(i));
    }
    return 0;
  }
}

Get Properties

using System;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
         "LDAP://192.168.1.100/cn=kblum, ou=sales, dc=ispnet1, dc=net");
      Console.WriteLine("object: {0}", de.Path);
      PropertyCollection pc = de.Properties;
      foreach(string propName in pc.PropertyNames)
      {
         foreach(object value in de.Properties[propName])
            Console.WriteLine("  property = {0}   value = {1}",
               propName, value);
      }
   }
}
object: LDAP://192.168.1.100/cn=kblum, ou=sales, dc=ispnet1, dc=net
^CTerminate batch job (Y/N)? n

Get Property Collection

using System;
using System.DirectoryServices;
public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    //Port 389 is LDAP Port
    MyDirectoryObject.Path = "LDAP://HMSRevenge:389/OU=Users,DC=Test,DC=COM";
    MyDirectoryObject.Username = @"Test\AAA";
    MyDirectoryObject.Password = @"MyPassword";
    PropertyCollection MyAttributes = MyDirectoryObject.Properties;
    foreach(string MyAttributeName in MyAttributes.PropertyNames)
    {
      PropertyValueCollection MyAttributeValues = MyAttributes[MyAttributeName];
      foreach(string MyValue in MyAttributeValues)
      {
        Console.WriteLine(MyAttributeName + " = " + MyValue);
      }
    }
    return 0;
  }
}

Get Property Collection using PropertyCache

using System;
using System.DirectoryServices;
public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyDirectoryObject = new DirectoryEntry();
    
    MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
    MyDirectoryObject.Username = @"AAA\BBB";
    MyDirectoryObject.Password = @"MyPassword";
    MyDirectoryObject.UsePropertyCache = false;
    DirectoryEntries MyChildObjects = MyDirectoryObject.Children;
        
    PropertyCollection MyAttributes = MyDirectoryObject.Properties;
    foreach(string MyAttributeName in MyAttributes.PropertyNames)
    {
      PropertyValueCollection MyAttributeValues = MyAttributes[MyAttributeName];
      foreach(string MyValue in MyAttributeValues)
      {
        Console.WriteLine(MyAttributeName + " = " + MyValue);
      }
    }
    return 0;
  }
}

Get Single Attribute

using System;
using System.Runtime.InteropServices;
using System.DirectoryServices;
using ActiveDs;
public unsafe class MainClass
{
  public static int Main(string[] args)
  {
    string Path = "LDAP://dsaddom.nttest.microsoft.ru/rootDSE";
    IADs pIADs = null;
    
    DirectoryEntry Entry = new DirectoryEntry(Path);
    pIADs = (IADs)Entry.NativeObject;
    string DefaultNamingContext = (string)pIADs.Get("defaultNamingContext");
    Console.WriteLine(DefaultNamingContext);
    
    Array NamingContext = (Array)pIADs.GetEx("defaultNamingContext");
    for(int i = 0; i < NamingContext.GetLength(0); i++)
    {
      Console.WriteLine((string)NamingContext.GetValue(i));
    }
    return 0;
  }
}

List Objects

using System;
using System.Collections;
using System.DirectoryServices;
class MainClass
{
   public static void Main()
   {
      DirectoryEntry de = new DirectoryEntry(
        "LDAP://192.168.1.100/dc=ispnet1, dc=net");
      Console.WriteLine(de.Path);
      DirectoryEntries des = de.Children;
      foreach(DirectoryEntry entry in des)
      {
         Console.WriteLine("  child: " + entry.Name);
      }
   }
}
LDAP://192.168.1.100/dc=ispnet1, dc=net
^CTerminate batch job (Y/N)? n

Reading Known Directory Objects

using System;
using System.DirectoryServices;
public class MainClass
{
  public static int Main(string[] args)
  {
    DirectoryEntry MyObject = new DirectoryEntry();
    MyObject.Path = "LDAP://HMSRevenge/OU=Users,DC=Test,DC=com";
    foreach(string MyValue in MyObject.Properties["email"])
      Console.WriteLine("Email" + " : " + MyValue);
    return 0;
  }
}

Retrieves Active Directory information

using System;
using System.DirectoryServices;
class MainClass
{
  public static void Main() 
  {
    DirectoryEntry de = new DirectoryEntry("WinNT://DomanName/MachineName", "Administrator", "Password");
    foreach(DirectoryEntry child in de.Children) 
    {
      Console.WriteLine(child.SchemaClassName + ": " + child.Name);
    }
  }
}