49

C#を使用してローカルのActiveDirectoryに接続したい。

私はこの良いドキュメントを見つけました。

しかし、LDAP経由で接続する方法がわかりません。

誰かが尋ねられたパラメータの使い方を説明できますか?

サンプルコード:

  static DirectoryEntry createDirectoryEntry()  
  {  
     // create and return new LDAP connection with desired settings  

     DirectoryEntry ldapConnection     = new DirectoryEntry("rizzo.leeds-art.ac.uk");  
     ldapConnection.Path               = "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk";  
     ldapConnection.AuthenticationType = AuthenticationTypes.Secure;  
     return ldapConnection;  
  }  

ActiveDirectoryサーバーのホスト名とIPアドレスがあります。などはどういうDC=xxx,DC=xx意味ですか?

4

3 に答える 3

94

DCはあなたのドメインです。DCよりもドメインexample.comに接続する場合は、DC = example、DC=comです。

実際には、ドメインコントローラーのホスト名やIPアドレスは必要ありません(それらはたくさんある可能性があります)。

ドメイン自体に接続していると想像してみてください。したがって、ドメインexample.comに接続するには、次のように記述します。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");

これで完了です。

接続に使用するユーザーとパスワードを指定することもできます。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com", "username", "password");

また、LDAPは必ず大文字で記述してください。どこかで大文字で書こうとすると問題が解決するまで、いくつかの問題と奇妙な例外がありました。

プロパティを使用すると、directoryEntry.Pathドメインをさらに深く掘り下げることができます。したがって、特定のOU(組織単位)内のユーザーを検索する場合は、そこに設定できます。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
directoryEntry.Path = "LDAP://OU=Specific Users,OU=All Users,OU=Users,DC=example,DC=com";

これは、次のAD階層と一致します。

  • com
      • ユーザー
        • 全てのユーザー
          • 特定のユーザー

階層を最も深いものから最も高いものへと単純に記述します。

今、あなたはたくさんのことをすることができます

たとえば、アカウント名でユーザーを検索し、ユーザーの名前を取得します。

DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://example.com");
DirectorySearcher searcher = new DirectorySearcher(directoryEntry) {
    PageSize = int.MaxValue,
    Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=AnAccountName))"
};

searcher.PropertiesToLoad.Add("sn");

var result = searcher.FindOne();

if (result == null) {
    return; // Or whatever you need to do in this case
}

string surname;

if (result.Properties.Contains("sn")) {
    surname = result.Properties["sn"][0].ToString();
}
于 2013-02-11T14:48:56.433 に答える
3

ldapConnectionはサーバーアドレスです。ldap.example.comLdap.Connection.Pathは、LDAP形式の挿入を使用するADS内のパスです。

OU = Your_OU、OU = other_ou、dc = example、dc = com

ADのルートに戻って作業する最も深いOUから開始し、トップレベルドメインを含むすべてが揃うまで、すべてのドメインセクションにdc=Xを追加します。

今、私は認証するパラメータを逃しています、これはユーザー名のパスと同じように機能します

CN = username、OU = users、DC = example、DC = com

LDAPの概要

于 2013-02-11T14:04:38.347 に答える
1

メールアドレスが「myname@mydomain.com」の場合は、createDirectoryEntry()を次のように変更してみてください。

XYZは、mydomainディレクトリに存在する場合のオプションのパラメータです。

static DirectoryEntry createDirectoryEntry()
{
    // create and return new LDAP connection with desired settings
    DirectoryEntry ldapConnection = new DirectoryEntry("myname.mydomain.com");
    ldapConnection.Path = "LDAP://OU=Users, OU=XYZ,DC=mydomain,DC=com";
    ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
    return ldapConnection;
}

これは基本的にcom->mydomain->XYZ->Users->abcdをチェックします

主な機能は次のとおりです。

try
{
    username = "Firstname LastName"
    DirectoryEntry myLdapConnection = createDirectoryEntry();
    DirectorySearcher search = new DirectorySearcher(myLdapConnection);
    search.Filter = "(cn=" + username + ")";
    ....    
于 2015-10-23T19:22:29.267 に答える