2

現在、ここにはいくつかの方法があります。

public ADHelper()
        {

           connection =  InitializeConnection();

        }

        private DirectoryEntry InitializeConnection()
        {
            DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://servername.domain.com:389/DC=domain,DC=com");
            ldapConnection.Username = "user"
            ldapConnection.Password = "password";

            ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
            return ldapConnection;

        }

そのドメイン内にオブジェクトが存在するかどうかを確認する別のメソッドを作成したいと思います。私は現在、次のことを行っています。

public bool Exists(string objectPath)
        {
            bool found = DirectoryEntry.Exists("LDAP://" + objectPath);
            return found;
        }

しかし、それでは LDAP 文字列全体を指定する必要があります。Exists() メソッド内の OU とおそらく CN パラメータを使用して、最初の ldapConnection を単純に拡張したいと思います。Initialize() メソッドを公開せずにこれを実現する方法はありますか?

本当にありがとう!

4

1 に答える 1

0

多分このようなもの:

public bool AccountExists(string userEmail)
{
    using (var root = GetLdapRoot())
    {
        using (var searcher = new DirectorySearcher(root))
        {
            searcher.Filter = string.Format("(&(objectClass=User)(mail={0}))", userEmail);
            searcher.PropertiesToLoad.Add("email");
            var result = searcher.FindAll();
            return result.Count > 0;
        }
    }
}


private static DirectoryEntry GetLdapRoot()
{
    return new DirectoryEntry("LDAP://DC=com"); //or whatever your root domain is. Set credentials if you need to
}

フィルターを設定し、読み込むプロパティを特定することで、検索がより効率的になります。ルートを LDAP:// 文字列として使用すると、ディレクトリ全体を検索する必要があります。

于 2012-09-04T21:53:48.550 に答える