2013-02-11 35 views
35

C#でローカルのActive Directoryに接続したいとします。LDAP経由でActive Directoryに接続

this good documentationが見つかりました。

しかし、実際に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; 
    } 

私はちょうどホスト名および当社のActive DirectoryサーバのIPアドレスを持っています。 DC=xxx,DC=xxなどはどういう意味ですか?

+4

ou =組織単位、dc =ドメインコンポーネント – paul

答えて

59

DCは、ドメインです。 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階層一致します

  • コム
      • ユーザーを
        • すべてのユーザー
          • 特定のユーザー

    単に最も深いから最高に階層を書く

Now you can do plenty of things

例えばアカウント名でユーザーを検索し、その姓を取得:

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(); 
} 
+1

これは答えです!ご協力ありがとうございました。 –

+0

私はIpAddressとFQNを持っています。どちらが速いの? 1 IpAddressには1つ以上のドメインが審査されますか? –

+0

フィルタを次のように単純化するまで、私はあらゆる種類の例外に遭遇しました: '"(cn = roland) "'。作業システムから、フィルターを徐々に「より良く」(=より複雑に)作ることができます – Roland

2

ldapConnectionはサーバーですadres:ldap.example.com Ldap.Connection.Pathは、LDAP形式で挿入するのが好きなADS内のパスです。

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

あなたは= Xのすべてのドメインのための最も深いOUバックADのルート営業、その後、追加DCで開始あなたは

は、今私は、パラメータを認証するために欠場トップレベルドメインを含むすべてのものを持っているまでのセクションでは、これは、ユーザ名

CN =ユーザー名、OU =ユーザー、DC =例えばパスと同じように動作しますDC = com

Introduction to LDAP

0

あなたのメールアドレスは「myname[email protected]」である場合、以下のように)createDirectoryEntryを(変更してみてください。それはMYDOMAINディレクトリに存在する場合

XYZはオプションのパラメータです

これは、基本的には、COMをチェックします
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; 
} 

- > MYDOMAIN - > XYZ - >ユーザー - > ABCD主な機能は以下のように見えます

try 
{ 
    username = "Firstname LastName" 
    DirectoryEntry myLdapConnection = createDirectoryEntry(); 
    DirectorySearcher search = new DirectorySearcher(myLdapConnection); 
    search.Filter = "(cn=" + username + ")"; 
    ....  
関連する問題