Csharp/CSharp Tutorial/Directory Services/Bind

Материал из .Net Framework эксперт
Версия от 15:20, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Anonymous bind

<source lang="csharp">using System; using System.DirectoryServices; public class DirectoryBinding {

 public static int Main(string[] args)
 {
   DirectoryEntry MyDirectoryObject = new DirectoryEntry("LDAP://HMSRevenge/rootDSE");
   
   return 0;
 }

}</source>

Binding use a specified user

<source lang="csharp">using System; using System.DirectoryServices; public class DirectoryBinding {

 public static int Main(string[] args)
 {
   DirectoryEntry MyDirectoryObject = new DirectoryEntry();
   
   MyDirectoryObject.Path = "LDAP://HMSRevenge/rootDSE";
   MyDirectoryObject.Username = @"Test\yourname";
   MyDirectoryObject.Password = @"MyPassword";
   return 0;
 }

}</source>

Bind Object

<source lang="csharp">using System; using System.DirectoryServices; class MainClass {

  public static void Main()
  {
     DirectoryEntry de = new DirectoryEntry(
         "LDAP://192.168.1.100/dc=ispnet1, dc=net");
     string ldappath = de.Path;
     Console.WriteLine("The LDAP path is: {0}", ldappath);
     de.Close();
  }

}</source>

The LDAP path is: LDAP://192.168.1.100/dc=ispnet1, dc=net

Directory Binding

<source lang="csharp">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 = @"userName\AAAA";
   MyDirectoryObject.Password = @"MyPassword";
   DirectoryEntries MyChildObjects = MyDirectoryObject.Children;
   return 0;  
 }

}</source>

Simple Bind: Use ASCII

<source lang="csharp">using System; using System.Runtime.InteropServices; using LDAP32; unsafe class MainClass {

 private static uint LDAP_PORT = 389;
 [STAThread]
 static void Main(string[] args)
 {
   LDAP32.LDAP* pLdap = null;
   sbyte* pServer = (sbyte*)Marshal.StringToCoTaskMemAnsi("dsaddom.nttest.microsoft.ru");
   sbyte* pName = (sbyte*)Marshal.StringToCoTaskMemAnsi("CN=Greg MacBeth,CN=Users,DC=dsaddom,DC=nttest,DC=MICROSOFT,DC=COM");
   sbyte* pPassword = (sbyte*)Marshal.StringToCoTaskMemAnsi("gregmcb");
   uint Result = 0;
       pLdap = Wldap32.ldap_init(pServer, LDAP_PORT);
   if(pLdap != null)
   {
     Result = Wldap32.ldap_simple_bind_s(pLdap, pName, pPassword);
   }
   Wldap32.ldap_unbind(pLdap);
 }

}</source>