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();
}
ou =組織単位、dc =ドメインコンポーネント – paul