2016-10-25 4 views
0

C#を使用して、現在の組織内のすべてのユーザーの詳細(名前、電子メール、指定、部署)が必要 C# LDAP query to retrieve all users in an organisational unit と 私は私が私のLDAPで間違ってやっているかを知る必要があり"A referral was returned from the server" exception when accessing AD from C#は、私は、このリンクをたどってきた

パス?

// create your domain context and define what container to search in - here OU=Employees 
     PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "MS", "OU=Employees,DC=CompanyName,DC=com"); 

     // define a "query-by-example" principal - here, we search for a UserPrincipal 
     // that is still active 
     UserPrincipal qbeUser = new UserPrincipal(ctx); 
     qbeUser.Enabled = true; 

     // create your principal searcher passing in the QBE principal  
     PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

     // find all matches 
     foreach (var found in srch.FindAll()) 
     { 
      // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
     } 

私はC#を使用して、現在の組織内のすべてのユーザーの詳細(名前、電子メール、指定、部署)が必要とドロップダウンリストでそれらを表示します。 助けてください。

+0

LDAPパスで何が間違っているのかを知る必要がありますか? – Sak

+0

あなたのコードによれば、 'PrincipalContext'コンストラクタへの第2のパラメータとして' MS'を送ります。これは[文書化されています](https://msdn.microsoft.com/en-us/library/bb348316(v = vs.110).aspx)_ "ドメインコンテキストタイプのドメインまたはサーバーの名前" _ - 正しいドメインサーバー名ですか? – stuartd

+0

私はMSドメインを持つユーザーが必要なので、はいと思います。私たちのシステムにログインするときは、ms /ユーザー名とパスワードを使用します。私はすべてのMSドメインユーザーのリストを必要としています。 MSが間違っていて、正しいものを知る必要がある場合は、どうすればわかりますか? – Sak

答えて

0
public DataTable FindPersons(string lname, string fname) 
    { 

     DirectorySearcher searcher = new DirectorySearcher(); 
     searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenname={0}*)(sn={1}*))", fname, lname); 

     SearchResultCollection allResults; 
     allResults = searcher.FindAll(); 

     DataTable dt = new DataTable(); 
     dt.Columns.Add("DisplayName", typeof(string)); 
     dt.Columns.Add("GivenName", typeof(string)); 
     dt.Columns.Add("SurName", typeof(string)); 
     dt.Columns.Add("MSID", typeof(string)); 
     if (allResults.Count >= 0) 
     { 
      for (int i = 0; i < allResults.Count; i++) 
      { 
       DirectoryEntry deMembershipUser = allResults[i].GetDirectoryEntry(); 
       deMembershipUser.RefreshCache(); 

       dt.Rows.Add(
        (string)deMembershipUser.Properties["displayname"].Value, 
        (string)deMembershipUser.Properties["givenName"].Value, 
        (string)deMembershipUser.Properties["sn"].Value, 
        (string)deMembershipUser.Properties["cn"].Value 
        ); 
      } 
     } 
     return dt; 
    } 
関連する問題