Csharp/CSharp Tutorial/Directory Services/Bind

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

Anonymous bind

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

Binding use a specified user

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;
  }
}

Bind Object

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();
   }
}
The LDAP path is: LDAP://192.168.1.100/dc=ispnet1, dc=net

Directory Binding

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;  
  }
}

Simple Bind: Use ASCII

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);
  }
}