1

私はstackoverflowですでに多くのメソッドが与えられていることを知っていますが、私の場合、それらすべてに時間がかかりすぎます。時間のかからないメソッドを投稿しましたが、それでも実装するには長すぎます。実行時間が短くなるように助けてください。また、.net2.0フレームワークを使用していることも考慮してください。

        try
        {
            List<string> lstEmails = new List<string>();
            string filter1 = string.Format("(anr={0})", "groupname");
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.Filter = filter1;
            searcher.SearchScope = SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("mail");
            IEnumerable res = (IEnumerable)searcher.FindOne().GetDirectoryEntry().Invoke("members");
            //IEnumerable<string> rest = (IEnumerable<string>)res;

            if (res != null)
            {
                try
                {
                    int index = 0;
                    foreach (IEnumerable resl in res)
                    {
                        DateTime start = DateTime.Now;
                        DirectoryEntry dr = new DirectoryEntry(resl);
                        string strEmail = null;
                        if (dr.Properties["mail"].Value != null)
                        {
                            strEmail = dr.Properties["mail"].Value.ToString();
                            Console.WriteLine(strEmail);
                            DateTime stop = DateTime.Now;
                            Console.WriteLine((stop - start).TotalMinutes.ToString());
                            index++;
                            Console.WriteLine(index.ToString());
                        }
                        if (!string.IsNullOrEmpty(strEmail))
                        {
                            // groupMemebers.Add("sam",strEmail);
                        }
                    }
                }
                catch { }
            }


        }
        catch { }

これはあなたが提案する方法です。

    DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain, "domainname" + strLDAPUserName, strLDAPPassword);
        DomainController controller = DomainController.FindOne(context);
        DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://{0}",controller.Domain), strLDAPUserName, strLDAPPassword, AuthenticationTypes.Secure);
List<string> userList = new List<string>();  
        DateTime StartTime = DateTime.Now;   
        using (DirectorySearcher ds = new DirectorySearcher(entry)) 
        {
            ds.PropertiesToLoad.Add("mail");  
            ds.PageSize = 10000;
            string DistingushiedName = "CN=" + groupName + ",OU=Users,dc=CompanyName,dc=com";
            ds.Filter = "(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:="+DistingushiedName+"))";   
            ds.SearchScope = SearchScope.Subtree; 
            try 
            {
                foreach (SearchResult user in ds.FindAll())   
                {
                    try  
                    {
                        userList.Add(user.Path);//.Properties["mail"][0].ToString()); 
                    }
                    catch (Exception E)    
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
            catch(Exception E)    
            {
                throw new Exception(E.Message); 
            }
            DateTime EndTime = DateTime.Now;
            TimeSpan Dif = EndTime.Subtract(StartTime);
        } 
4

3 に答える 3

0

非常に簡単です(ADが2003 R2以降の場合):

        List<string> userList = new List<string>();
        DateTime StartTime = DateTime.Now;
        using (DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry ("GC://DC=YourDomain,DC=com")))
        {
            ds.PropertiesToLoad.Add("mail");
            ds.PageSize = 10000;
            ds.Filter = "(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:=YOUR_GROUP'S DN))";
            ds.SearchScope = SearchScope.Subtree;
            try
            {
                foreach (SearchResult user in ds.FindAll())
                {
                    try
                    {
                        userList.Add(user.Path);//.Properties["mail"][0].ToString());
                    }
                    catch (Exception E)
                    {
                        throw new Exception(E.Message);
                    }
                }
            }
            catch(Exception E) 
            {
                throw new Exception(E.Message);
            }
            DateTime EndTime = DateTime.Now;
            TimeSpan Dif = EndTime.Subtract(StartTime);

        }

YOUR_GROUPのDNをグループの識別名に置き換えます...

memberof:1.2.840.113556.1.4.1941:=「新しい」LDAP_MATCHING_RULE_IN_CHAIN演算子であり、すべてのグループメンバーを取得します。ADの準備ができているかどうかを確認し、詳細情報を入手するには、こちらをご覧ください。

編集:

私はあなたに答えを与えました、しかし説明はさらに役立つかもしれません。

一般に、ANR検索は大きなワイルドカードORクエリに拡張されるため、避ける必要があります。検索している名前がどのプロパティに含まれているかわからない場合にのみ使用してください。明示的なAND検索よりもはるかに低速です

次に、複数のドメインがある場合は、ヒットするまですべてのドメインを検索する場合を除いて、紹介追跡をオフにする必要があります。この場合、探しているオブジェクトを見つけるためにLDAP://検索よりもGC://を実行する方が、そのオブジェクトに対してLDAP検索を実行するよりも優れています。探しているものによっては、GCクエリで十分な場合があります

編集2:

より多くのエラー情報を提供し、電子メールの代わりにユーザーパスを取得するようにコードを変更しました。

于 2012-10-08T17:40:11.680 に答える
0

これがあなたの解決策です:-

string[] email = new string[0];

DirectoryEntry entry = new DirectoryEntry("LDAP://OU=Users,dc=me,dc=com", username, password);
string groupName = "GroupName";//Group NAme

DirectorySearcher groupSearch = new DirectorySearcher(entry);
groupSearch.Filter = "(SAMAccountName=" + groupName + ")";
groupSearch.PropertiesToLoad.Add("member");
SearchResult groupResult = groupSearch.FindOne(); // getting the members who belongs to the concern groupname

if (groupResult != null)
 {
    email = new string[groupResult.Properties["member"].Count];  //creatign an array to store all the email address
    for (int iSearchLoop = 0; iSearchLoop < groupResult.Properties["member"].Count; iSearchLoop++)
      {
         string userName = groupResult.Properties["member"][iSearchLoop].ToString();
         int index = userName.IndexOf(',');
         userName = userName.Substring(0, index).Replace("CN=", "").ToString(); // the name of the user will be fetched.

         DirectorySearcher search = new DirectorySearcher(entry);
         search.Filter = "(name=" + userName + ")";
         search.PropertiesToLoad.Add("mail");
         SearchResult result = search.FindOne(); //finding the mail id
         if (result != null)
          {
            email[iSearchLoop] = result.Properties["mail"][0].ToString(); //assigning the mail id to an array....
          }
      }
}

これがお役に立てば幸いです

于 2012-10-08T13:49:45.697 に答える
0

ねえ、これは正しい方法です...

  try
        {
            List<string> ReturnArray = new List<string>();
            DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domainName, domainName + "\\" + UserName, Password);
            DomainController controller = DomainController.FindOne(context);
            string LDAPAddress = string.Format("LDAP://{0}", controller.Domain);
            DirectoryEntry deDirEntry = new DirectoryEntry(LDAPAddress, UserName, Password);
            deDirEntry.AuthenticationType = AuthenticationTypes.Secure;

            DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
            mySearcher.PropertiesToLoad.Add("distinguishedName");
            string sFilter = String.Format("(&(objectcategory=group)(cn=" + GroupName + "))");

            mySearcher.Filter = sFilter;
            mySearcher.Sort.Direction = SortDirection.Ascending;
            mySearcher.Sort.PropertyName = "cn";
            SearchResult result;
            DirectoryEntry ResultEntry;
            result = mySearcher.FindOne();
            ResultEntry = result.GetDirectoryEntry();
            GroupName = ResultEntry.Properties["distinguishedName"].Value.ToString();
            mySearcher = new DirectorySearcher(deDirEntry);
            mySearcher.PropertiesToLoad.Add("cn");
            mySearcher.PropertiesToLoad.Add("mail");
            sFilter = String.Format("(&(objectClass=person)(memberOf={0}))", GroupName);
            mySearcher.Filter = sFilter;
            mySearcher.Sort.Direction = SortDirection.Ascending;
            mySearcher.Sort.PropertyName = "cn";
            SearchResultCollection results;
            results = mySearcher.FindAll();
            foreach (SearchResult resEnt in results)
            {
                ResultPropertyCollection propcoll = resEnt.Properties;
                foreach (string key in propcoll.PropertyNames)
                {
                    if (key == "mail")
                    {
                        foreach (object values in propcoll[key])
                        {
                            if (!String.IsNullOrEmpty(values.ToString()))
                            {
                                ReturnArray.Add(values.ToString());
                                Console.WriteLine(values.ToString());
                            }
                        }
                    }
                }
            }

            return ReturnArray;
        }
        catch
        {
            return null;
        }

貴重なご提案をありがとうございました

于 2012-10-10T14:42:14.533 に答える